grcan.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Socket CAN driver for Aeroflex Gaisler GRCAN and GRHCAN.
  4. *
  5. * 2012 (c) Aeroflex Gaisler AB
  6. *
  7. * This driver supports GRCAN and GRHCAN CAN controllers available in the GRLIB
  8. * VHDL IP core library.
  9. *
  10. * Full documentation of the GRCAN core can be found here:
  11. * http://www.gaisler.com/products/grlib/grip.pdf
  12. *
  13. * See "Documentation/devicetree/bindings/net/can/grcan.txt" for information on
  14. * open firmware properties.
  15. *
  16. * See "Documentation/ABI/testing/sysfs-class-net-grcan" for information on the
  17. * sysfs interface.
  18. *
  19. * See "Documentation/admin-guide/kernel-parameters.rst" for information on the module
  20. * parameters.
  21. *
  22. * Contributors: Andreas Larsson <andreas@gaisler.com>
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/delay.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/io.h>
  31. #include <linux/can/dev.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/of.h>
  35. #include <linux/of_irq.h>
  36. #include <linux/dma-mapping.h>
  37. #define DRV_NAME "grcan"
  38. #define GRCAN_NAPI_WEIGHT 32
  39. #define GRCAN_RESERVE_SIZE(slot1, slot2) (((slot2) - (slot1)) / 4 - 1)
  40. struct grcan_registers {
  41. u32 conf; /* 0x00 */
  42. u32 stat; /* 0x04 */
  43. u32 ctrl; /* 0x08 */
  44. u32 __reserved1[GRCAN_RESERVE_SIZE(0x08, 0x18)];
  45. u32 smask; /* 0x18 - CanMASK */
  46. u32 scode; /* 0x1c - CanCODE */
  47. u32 __reserved2[GRCAN_RESERVE_SIZE(0x1c, 0x100)];
  48. u32 pimsr; /* 0x100 */
  49. u32 pimr; /* 0x104 */
  50. u32 pisr; /* 0x108 */
  51. u32 pir; /* 0x10C */
  52. u32 imr; /* 0x110 */
  53. u32 picr; /* 0x114 */
  54. u32 __reserved3[GRCAN_RESERVE_SIZE(0x114, 0x200)];
  55. u32 txctrl; /* 0x200 */
  56. u32 txaddr; /* 0x204 */
  57. u32 txsize; /* 0x208 */
  58. u32 txwr; /* 0x20C */
  59. u32 txrd; /* 0x210 */
  60. u32 txirq; /* 0x214 */
  61. u32 __reserved4[GRCAN_RESERVE_SIZE(0x214, 0x300)];
  62. u32 rxctrl; /* 0x300 */
  63. u32 rxaddr; /* 0x304 */
  64. u32 rxsize; /* 0x308 */
  65. u32 rxwr; /* 0x30C */
  66. u32 rxrd; /* 0x310 */
  67. u32 rxirq; /* 0x314 */
  68. u32 rxmask; /* 0x318 */
  69. u32 rxcode; /* 0x31C */
  70. };
  71. #define GRCAN_CONF_ABORT 0x00000001
  72. #define GRCAN_CONF_ENABLE0 0x00000002
  73. #define GRCAN_CONF_ENABLE1 0x00000004
  74. #define GRCAN_CONF_SELECT 0x00000008
  75. #define GRCAN_CONF_SILENT 0x00000010
  76. #define GRCAN_CONF_SAM 0x00000020 /* Available in some hardware */
  77. #define GRCAN_CONF_BPR 0x00000300 /* Note: not BRP */
  78. #define GRCAN_CONF_RSJ 0x00007000
  79. #define GRCAN_CONF_PS1 0x00f00000
  80. #define GRCAN_CONF_PS2 0x000f0000
  81. #define GRCAN_CONF_SCALER 0xff000000
  82. #define GRCAN_CONF_OPERATION \
  83. (GRCAN_CONF_ABORT | GRCAN_CONF_ENABLE0 | GRCAN_CONF_ENABLE1 \
  84. | GRCAN_CONF_SELECT | GRCAN_CONF_SILENT | GRCAN_CONF_SAM)
  85. #define GRCAN_CONF_TIMING \
  86. (GRCAN_CONF_BPR | GRCAN_CONF_RSJ | GRCAN_CONF_PS1 \
  87. | GRCAN_CONF_PS2 | GRCAN_CONF_SCALER)
  88. #define GRCAN_CONF_RSJ_MIN 1
  89. #define GRCAN_CONF_RSJ_MAX 4
  90. #define GRCAN_CONF_PS1_MIN 1
  91. #define GRCAN_CONF_PS1_MAX 15
  92. #define GRCAN_CONF_PS2_MIN 2
  93. #define GRCAN_CONF_PS2_MAX 8
  94. #define GRCAN_CONF_SCALER_MIN 0
  95. #define GRCAN_CONF_SCALER_MAX 255
  96. #define GRCAN_CONF_SCALER_INC 1
  97. #define GRCAN_CONF_BPR_BIT 8
  98. #define GRCAN_CONF_RSJ_BIT 12
  99. #define GRCAN_CONF_PS1_BIT 20
  100. #define GRCAN_CONF_PS2_BIT 16
  101. #define GRCAN_CONF_SCALER_BIT 24
  102. #define GRCAN_STAT_PASS 0x000001
  103. #define GRCAN_STAT_OFF 0x000002
  104. #define GRCAN_STAT_OR 0x000004
  105. #define GRCAN_STAT_AHBERR 0x000008
  106. #define GRCAN_STAT_ACTIVE 0x000010
  107. #define GRCAN_STAT_RXERRCNT 0x00ff00
  108. #define GRCAN_STAT_TXERRCNT 0xff0000
  109. #define GRCAN_STAT_ERRCTR_RELATED (GRCAN_STAT_PASS | GRCAN_STAT_OFF)
  110. #define GRCAN_STAT_RXERRCNT_BIT 8
  111. #define GRCAN_STAT_TXERRCNT_BIT 16
  112. #define GRCAN_STAT_ERRCNT_WARNING_LIMIT 96
  113. #define GRCAN_STAT_ERRCNT_PASSIVE_LIMIT 127
  114. #define GRCAN_CTRL_RESET 0x2
  115. #define GRCAN_CTRL_ENABLE 0x1
  116. #define GRCAN_TXCTRL_ENABLE 0x1
  117. #define GRCAN_TXCTRL_ONGOING 0x2
  118. #define GRCAN_TXCTRL_SINGLE 0x4
  119. #define GRCAN_RXCTRL_ENABLE 0x1
  120. #define GRCAN_RXCTRL_ONGOING 0x2
  121. /* Relative offset of IRQ sources to AMBA Plug&Play */
  122. #define GRCAN_IRQIX_IRQ 0
  123. #define GRCAN_IRQIX_TXSYNC 1
  124. #define GRCAN_IRQIX_RXSYNC 2
  125. #define GRCAN_IRQ_PASS 0x00001
  126. #define GRCAN_IRQ_OFF 0x00002
  127. #define GRCAN_IRQ_OR 0x00004
  128. #define GRCAN_IRQ_RXAHBERR 0x00008
  129. #define GRCAN_IRQ_TXAHBERR 0x00010
  130. #define GRCAN_IRQ_RXIRQ 0x00020
  131. #define GRCAN_IRQ_TXIRQ 0x00040
  132. #define GRCAN_IRQ_RXFULL 0x00080
  133. #define GRCAN_IRQ_TXEMPTY 0x00100
  134. #define GRCAN_IRQ_RX 0x00200
  135. #define GRCAN_IRQ_TX 0x00400
  136. #define GRCAN_IRQ_RXSYNC 0x00800
  137. #define GRCAN_IRQ_TXSYNC 0x01000
  138. #define GRCAN_IRQ_RXERRCTR 0x02000
  139. #define GRCAN_IRQ_TXERRCTR 0x04000
  140. #define GRCAN_IRQ_RXMISS 0x08000
  141. #define GRCAN_IRQ_TXLOSS 0x10000
  142. #define GRCAN_IRQ_NONE 0
  143. #define GRCAN_IRQ_ALL \
  144. (GRCAN_IRQ_PASS | GRCAN_IRQ_OFF | GRCAN_IRQ_OR \
  145. | GRCAN_IRQ_RXAHBERR | GRCAN_IRQ_TXAHBERR \
  146. | GRCAN_IRQ_RXIRQ | GRCAN_IRQ_TXIRQ \
  147. | GRCAN_IRQ_RXFULL | GRCAN_IRQ_TXEMPTY \
  148. | GRCAN_IRQ_RX | GRCAN_IRQ_TX | GRCAN_IRQ_RXSYNC \
  149. | GRCAN_IRQ_TXSYNC | GRCAN_IRQ_RXERRCTR \
  150. | GRCAN_IRQ_TXERRCTR | GRCAN_IRQ_RXMISS \
  151. | GRCAN_IRQ_TXLOSS)
  152. #define GRCAN_IRQ_ERRCTR_RELATED (GRCAN_IRQ_RXERRCTR | GRCAN_IRQ_TXERRCTR \
  153. | GRCAN_IRQ_PASS | GRCAN_IRQ_OFF)
  154. #define GRCAN_IRQ_ERRORS (GRCAN_IRQ_ERRCTR_RELATED | GRCAN_IRQ_OR \
  155. | GRCAN_IRQ_TXAHBERR | GRCAN_IRQ_RXAHBERR \
  156. | GRCAN_IRQ_TXLOSS)
  157. #define GRCAN_IRQ_DEFAULT (GRCAN_IRQ_RX | GRCAN_IRQ_TX | GRCAN_IRQ_ERRORS)
  158. #define GRCAN_MSG_SIZE 16
  159. #define GRCAN_MSG_IDE 0x80000000
  160. #define GRCAN_MSG_RTR 0x40000000
  161. #define GRCAN_MSG_BID 0x1ffc0000
  162. #define GRCAN_MSG_EID 0x1fffffff
  163. #define GRCAN_MSG_IDE_BIT 31
  164. #define GRCAN_MSG_RTR_BIT 30
  165. #define GRCAN_MSG_BID_BIT 18
  166. #define GRCAN_MSG_EID_BIT 0
  167. #define GRCAN_MSG_DLC 0xf0000000
  168. #define GRCAN_MSG_TXERRC 0x00ff0000
  169. #define GRCAN_MSG_RXERRC 0x0000ff00
  170. #define GRCAN_MSG_DLC_BIT 28
  171. #define GRCAN_MSG_TXERRC_BIT 16
  172. #define GRCAN_MSG_RXERRC_BIT 8
  173. #define GRCAN_MSG_AHBERR 0x00000008
  174. #define GRCAN_MSG_OR 0x00000004
  175. #define GRCAN_MSG_OFF 0x00000002
  176. #define GRCAN_MSG_PASS 0x00000001
  177. #define GRCAN_MSG_DATA_SLOT_INDEX(i) (2 + (i) / 4)
  178. #define GRCAN_MSG_DATA_SHIFT(i) ((3 - (i) % 4) * 8)
  179. #define GRCAN_BUFFER_ALIGNMENT 1024
  180. #define GRCAN_DEFAULT_BUFFER_SIZE 1024
  181. #define GRCAN_VALID_TR_SIZE_MASK 0x001fffc0
  182. #define GRCAN_INVALID_BUFFER_SIZE(s) \
  183. ((s) == 0 || ((s) & ~GRCAN_VALID_TR_SIZE_MASK))
  184. #if GRCAN_INVALID_BUFFER_SIZE(GRCAN_DEFAULT_BUFFER_SIZE)
  185. #error "Invalid default buffer size"
  186. #endif
  187. struct grcan_dma_buffer {
  188. size_t size;
  189. void *buf;
  190. dma_addr_t handle;
  191. };
  192. struct grcan_dma {
  193. size_t base_size;
  194. void *base_buf;
  195. dma_addr_t base_handle;
  196. struct grcan_dma_buffer tx;
  197. struct grcan_dma_buffer rx;
  198. };
  199. /* GRCAN configuration parameters */
  200. struct grcan_device_config {
  201. unsigned short enable0;
  202. unsigned short enable1;
  203. unsigned short select;
  204. unsigned int txsize;
  205. unsigned int rxsize;
  206. };
  207. #define GRCAN_DEFAULT_DEVICE_CONFIG { \
  208. .enable0 = 0, \
  209. .enable1 = 0, \
  210. .select = 0, \
  211. .txsize = GRCAN_DEFAULT_BUFFER_SIZE, \
  212. .rxsize = GRCAN_DEFAULT_BUFFER_SIZE, \
  213. }
  214. #define GRCAN_TXBUG_SAFE_GRLIB_VERSION 4100
  215. #define GRLIB_VERSION_MASK 0xffff
  216. /* GRCAN private data structure */
  217. struct grcan_priv {
  218. struct can_priv can; /* must be the first member */
  219. struct net_device *dev;
  220. struct device *ofdev_dev;
  221. struct napi_struct napi;
  222. struct grcan_registers __iomem *regs; /* ioremap'ed registers */
  223. struct grcan_device_config config;
  224. struct grcan_dma dma;
  225. struct sk_buff **echo_skb; /* We allocate this on our own */
  226. /* The echo skb pointer, pointing into echo_skb and indicating which
  227. * frames can be echoed back. See the "Notes on the tx cyclic buffer
  228. * handling"-comment for grcan_start_xmit for more details.
  229. */
  230. u32 eskbp;
  231. /* Lock for controlling changes to the netif tx queue state, accesses to
  232. * the echo_skb pointer eskbp and for making sure that a running reset
  233. * and/or a close of the interface is done without interference from
  234. * other parts of the code.
  235. *
  236. * The echo_skb pointer, eskbp, should only be accessed under this lock
  237. * as it can be changed in several places and together with decisions on
  238. * whether to wake up the tx queue.
  239. *
  240. * The tx queue must never be woken up if there is a running reset or
  241. * close in progress.
  242. *
  243. * A running reset (see below on need_txbug_workaround) should never be
  244. * done if the interface is closing down and several running resets
  245. * should never be scheduled simultaneously.
  246. */
  247. spinlock_t lock;
  248. /* Whether a workaround is needed due to a bug in older hardware. In
  249. * this case, the driver both tries to prevent the bug from being
  250. * triggered and recovers, if the bug nevertheless happens, by doing a
  251. * running reset. A running reset, resets the device and continues from
  252. * where it were without being noticeable from outside the driver (apart
  253. * from slight delays).
  254. */
  255. bool need_txbug_workaround;
  256. /* To trigger initization of running reset and to trigger running reset
  257. * respectively in the case of a hanged device due to a txbug.
  258. */
  259. struct timer_list hang_timer;
  260. struct timer_list rr_timer;
  261. /* To avoid waking up the netif queue and restarting timers
  262. * when a reset is scheduled or when closing of the device is
  263. * undergoing
  264. */
  265. bool resetting;
  266. bool closing;
  267. };
  268. /* Wait time for a short wait for ongoing to clear */
  269. #define GRCAN_SHORTWAIT_USECS 10
  270. /* Limit on the number of transmitted bits of an eff frame according to the CAN
  271. * specification: 1 bit start of frame, 32 bits arbitration field, 6 bits
  272. * control field, 8 bytes data field, 16 bits crc field, 2 bits ACK field and 7
  273. * bits end of frame
  274. */
  275. #define GRCAN_EFF_FRAME_MAX_BITS (1+32+6+8*8+16+2+7)
  276. #if defined(__BIG_ENDIAN)
  277. static inline u32 grcan_read_reg(u32 __iomem *reg)
  278. {
  279. return ioread32be(reg);
  280. }
  281. static inline void grcan_write_reg(u32 __iomem *reg, u32 val)
  282. {
  283. iowrite32be(val, reg);
  284. }
  285. #else
  286. static inline u32 grcan_read_reg(u32 __iomem *reg)
  287. {
  288. return ioread32(reg);
  289. }
  290. static inline void grcan_write_reg(u32 __iomem *reg, u32 val)
  291. {
  292. iowrite32(val, reg);
  293. }
  294. #endif
  295. static inline void grcan_clear_bits(u32 __iomem *reg, u32 mask)
  296. {
  297. grcan_write_reg(reg, grcan_read_reg(reg) & ~mask);
  298. }
  299. static inline void grcan_set_bits(u32 __iomem *reg, u32 mask)
  300. {
  301. grcan_write_reg(reg, grcan_read_reg(reg) | mask);
  302. }
  303. static inline u32 grcan_read_bits(u32 __iomem *reg, u32 mask)
  304. {
  305. return grcan_read_reg(reg) & mask;
  306. }
  307. static inline void grcan_write_bits(u32 __iomem *reg, u32 value, u32 mask)
  308. {
  309. u32 old = grcan_read_reg(reg);
  310. grcan_write_reg(reg, (old & ~mask) | (value & mask));
  311. }
  312. /* a and b should both be in [0,size] and a == b == size should not hold */
  313. static inline u32 grcan_ring_add(u32 a, u32 b, u32 size)
  314. {
  315. u32 sum = a + b;
  316. if (sum < size)
  317. return sum;
  318. else
  319. return sum - size;
  320. }
  321. /* a and b should both be in [0,size) */
  322. static inline u32 grcan_ring_sub(u32 a, u32 b, u32 size)
  323. {
  324. return grcan_ring_add(a, size - b, size);
  325. }
  326. /* Available slots for new transmissions */
  327. static inline u32 grcan_txspace(size_t txsize, u32 txwr, u32 eskbp)
  328. {
  329. u32 slots = txsize / GRCAN_MSG_SIZE - 1;
  330. u32 used = grcan_ring_sub(txwr, eskbp, txsize) / GRCAN_MSG_SIZE;
  331. return slots - used;
  332. }
  333. /* Configuration parameters that can be set via module parameters */
  334. static struct grcan_device_config grcan_module_config =
  335. GRCAN_DEFAULT_DEVICE_CONFIG;
  336. static const struct can_bittiming_const grcan_bittiming_const = {
  337. .name = DRV_NAME,
  338. .tseg1_min = GRCAN_CONF_PS1_MIN + 1,
  339. .tseg1_max = GRCAN_CONF_PS1_MAX + 1,
  340. .tseg2_min = GRCAN_CONF_PS2_MIN,
  341. .tseg2_max = GRCAN_CONF_PS2_MAX,
  342. .sjw_max = GRCAN_CONF_RSJ_MAX,
  343. .brp_min = GRCAN_CONF_SCALER_MIN + 1,
  344. .brp_max = GRCAN_CONF_SCALER_MAX + 1,
  345. .brp_inc = GRCAN_CONF_SCALER_INC,
  346. };
  347. static int grcan_set_bittiming(struct net_device *dev)
  348. {
  349. struct grcan_priv *priv = netdev_priv(dev);
  350. struct grcan_registers __iomem *regs = priv->regs;
  351. struct can_bittiming *bt = &priv->can.bittiming;
  352. u32 timing = 0;
  353. int bpr, rsj, ps1, ps2, scaler;
  354. /* Should never happen - function will not be called when
  355. * device is up
  356. */
  357. if (grcan_read_bits(&regs->ctrl, GRCAN_CTRL_ENABLE))
  358. return -EBUSY;
  359. bpr = 0; /* Note bpr and brp are different concepts */
  360. rsj = bt->sjw;
  361. ps1 = (bt->prop_seg + bt->phase_seg1) - 1; /* tseg1 - 1 */
  362. ps2 = bt->phase_seg2;
  363. scaler = (bt->brp - 1);
  364. netdev_dbg(dev, "Request for BPR=%d, RSJ=%d, PS1=%d, PS2=%d, SCALER=%d",
  365. bpr, rsj, ps1, ps2, scaler);
  366. if (!(ps1 > ps2)) {
  367. netdev_err(dev, "PS1 > PS2 must hold: PS1=%d, PS2=%d\n",
  368. ps1, ps2);
  369. return -EINVAL;
  370. }
  371. if (!(ps2 >= rsj)) {
  372. netdev_err(dev, "PS2 >= RSJ must hold: PS2=%d, RSJ=%d\n",
  373. ps2, rsj);
  374. return -EINVAL;
  375. }
  376. timing |= (bpr << GRCAN_CONF_BPR_BIT) & GRCAN_CONF_BPR;
  377. timing |= (rsj << GRCAN_CONF_RSJ_BIT) & GRCAN_CONF_RSJ;
  378. timing |= (ps1 << GRCAN_CONF_PS1_BIT) & GRCAN_CONF_PS1;
  379. timing |= (ps2 << GRCAN_CONF_PS2_BIT) & GRCAN_CONF_PS2;
  380. timing |= (scaler << GRCAN_CONF_SCALER_BIT) & GRCAN_CONF_SCALER;
  381. netdev_info(dev, "setting timing=0x%x\n", timing);
  382. grcan_write_bits(&regs->conf, timing, GRCAN_CONF_TIMING);
  383. return 0;
  384. }
  385. static int grcan_get_berr_counter(const struct net_device *dev,
  386. struct can_berr_counter *bec)
  387. {
  388. struct grcan_priv *priv = netdev_priv(dev);
  389. struct grcan_registers __iomem *regs = priv->regs;
  390. u32 status = grcan_read_reg(&regs->stat);
  391. bec->txerr = (status & GRCAN_STAT_TXERRCNT) >> GRCAN_STAT_TXERRCNT_BIT;
  392. bec->rxerr = (status & GRCAN_STAT_RXERRCNT) >> GRCAN_STAT_RXERRCNT_BIT;
  393. return 0;
  394. }
  395. static int grcan_poll(struct napi_struct *napi, int budget);
  396. /* Reset device, but keep configuration information */
  397. static void grcan_reset(struct net_device *dev)
  398. {
  399. struct grcan_priv *priv = netdev_priv(dev);
  400. struct grcan_registers __iomem *regs = priv->regs;
  401. u32 config = grcan_read_reg(&regs->conf);
  402. grcan_set_bits(&regs->ctrl, GRCAN_CTRL_RESET);
  403. grcan_write_reg(&regs->conf, config);
  404. priv->eskbp = grcan_read_reg(&regs->txrd);
  405. priv->can.state = CAN_STATE_STOPPED;
  406. /* Turn off hardware filtering - regs->rxcode set to 0 by reset */
  407. grcan_write_reg(&regs->rxmask, 0);
  408. }
  409. /* stop device without changing any configurations */
  410. static void grcan_stop_hardware(struct net_device *dev)
  411. {
  412. struct grcan_priv *priv = netdev_priv(dev);
  413. struct grcan_registers __iomem *regs = priv->regs;
  414. grcan_write_reg(&regs->imr, GRCAN_IRQ_NONE);
  415. grcan_clear_bits(&regs->txctrl, GRCAN_TXCTRL_ENABLE);
  416. grcan_clear_bits(&regs->rxctrl, GRCAN_RXCTRL_ENABLE);
  417. grcan_clear_bits(&regs->ctrl, GRCAN_CTRL_ENABLE);
  418. }
  419. /* Let priv->eskbp catch up to regs->txrd and echo back the skbs if echo
  420. * is true and free them otherwise.
  421. *
  422. * If budget is >= 0, stop after handling at most budget skbs. Otherwise,
  423. * continue until priv->eskbp catches up to regs->txrd.
  424. *
  425. * priv->lock *must* be held when calling this function
  426. */
  427. static int catch_up_echo_skb(struct net_device *dev, int budget, bool echo)
  428. {
  429. struct grcan_priv *priv = netdev_priv(dev);
  430. struct grcan_registers __iomem *regs = priv->regs;
  431. struct grcan_dma *dma = &priv->dma;
  432. struct net_device_stats *stats = &dev->stats;
  433. int i, work_done;
  434. /* Updates to priv->eskbp and wake-ups of the queue needs to
  435. * be atomic towards the reads of priv->eskbp and shut-downs
  436. * of the queue in grcan_start_xmit.
  437. */
  438. u32 txrd = grcan_read_reg(&regs->txrd);
  439. for (work_done = 0; work_done < budget || budget < 0; work_done++) {
  440. if (priv->eskbp == txrd)
  441. break;
  442. i = priv->eskbp / GRCAN_MSG_SIZE;
  443. if (echo) {
  444. /* Normal echo of messages */
  445. stats->tx_packets++;
  446. stats->tx_bytes += can_get_echo_skb(dev, i, NULL);
  447. } else {
  448. /* For cleanup of untransmitted messages */
  449. can_free_echo_skb(dev, i, NULL);
  450. }
  451. priv->eskbp = grcan_ring_add(priv->eskbp, GRCAN_MSG_SIZE,
  452. dma->tx.size);
  453. txrd = grcan_read_reg(&regs->txrd);
  454. }
  455. return work_done;
  456. }
  457. static void grcan_lost_one_shot_frame(struct net_device *dev)
  458. {
  459. struct grcan_priv *priv = netdev_priv(dev);
  460. struct grcan_registers __iomem *regs = priv->regs;
  461. struct grcan_dma *dma = &priv->dma;
  462. u32 txrd;
  463. unsigned long flags;
  464. spin_lock_irqsave(&priv->lock, flags);
  465. catch_up_echo_skb(dev, -1, true);
  466. if (unlikely(grcan_read_bits(&regs->txctrl, GRCAN_TXCTRL_ENABLE))) {
  467. /* Should never happen */
  468. netdev_err(dev, "TXCTRL enabled at TXLOSS in one shot mode\n");
  469. } else {
  470. /* By the time an GRCAN_IRQ_TXLOSS is generated in
  471. * one-shot mode there is no problem in writing
  472. * to TXRD even in versions of the hardware in
  473. * which GRCAN_TXCTRL_ONGOING is not cleared properly
  474. * in one-shot mode.
  475. */
  476. /* Skip message and discard echo-skb */
  477. txrd = grcan_read_reg(&regs->txrd);
  478. txrd = grcan_ring_add(txrd, GRCAN_MSG_SIZE, dma->tx.size);
  479. grcan_write_reg(&regs->txrd, txrd);
  480. catch_up_echo_skb(dev, -1, false);
  481. if (!priv->resetting && !priv->closing &&
  482. !(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)) {
  483. netif_wake_queue(dev);
  484. grcan_set_bits(&regs->txctrl, GRCAN_TXCTRL_ENABLE);
  485. }
  486. }
  487. spin_unlock_irqrestore(&priv->lock, flags);
  488. }
  489. static void grcan_err(struct net_device *dev, u32 sources, u32 status)
  490. {
  491. struct grcan_priv *priv = netdev_priv(dev);
  492. struct grcan_registers __iomem *regs = priv->regs;
  493. struct grcan_dma *dma = &priv->dma;
  494. struct net_device_stats *stats = &dev->stats;
  495. struct can_frame cf;
  496. /* Zero potential error_frame */
  497. memset(&cf, 0, sizeof(cf));
  498. /* Message lost interrupt. This might be due to arbitration error, but
  499. * is also triggered when there is no one else on the can bus or when
  500. * there is a problem with the hardware interface or the bus itself. As
  501. * arbitration errors can not be singled out, no error frames are
  502. * generated reporting this event as an arbitration error.
  503. */
  504. if (sources & GRCAN_IRQ_TXLOSS) {
  505. /* Take care of failed one-shot transmit */
  506. if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
  507. grcan_lost_one_shot_frame(dev);
  508. /* Stop printing as soon as error passive or bus off is in
  509. * effect to limit the amount of txloss debug printouts.
  510. */
  511. if (!(status & GRCAN_STAT_ERRCTR_RELATED)) {
  512. netdev_dbg(dev, "tx message lost\n");
  513. stats->tx_errors++;
  514. }
  515. }
  516. /* Conditions dealing with the error counters. There is no interrupt for
  517. * error warning, but there are interrupts for increases of the error
  518. * counters.
  519. */
  520. if ((sources & GRCAN_IRQ_ERRCTR_RELATED) ||
  521. (status & GRCAN_STAT_ERRCTR_RELATED)) {
  522. enum can_state state = priv->can.state;
  523. enum can_state oldstate = state;
  524. u32 txerr = (status & GRCAN_STAT_TXERRCNT)
  525. >> GRCAN_STAT_TXERRCNT_BIT;
  526. u32 rxerr = (status & GRCAN_STAT_RXERRCNT)
  527. >> GRCAN_STAT_RXERRCNT_BIT;
  528. /* Figure out current state */
  529. if (status & GRCAN_STAT_OFF) {
  530. state = CAN_STATE_BUS_OFF;
  531. } else if (status & GRCAN_STAT_PASS) {
  532. state = CAN_STATE_ERROR_PASSIVE;
  533. } else if (txerr >= GRCAN_STAT_ERRCNT_WARNING_LIMIT ||
  534. rxerr >= GRCAN_STAT_ERRCNT_WARNING_LIMIT) {
  535. state = CAN_STATE_ERROR_WARNING;
  536. } else {
  537. state = CAN_STATE_ERROR_ACTIVE;
  538. }
  539. /* Handle and report state changes */
  540. if (state != oldstate) {
  541. switch (state) {
  542. case CAN_STATE_BUS_OFF:
  543. netdev_dbg(dev, "bus-off\n");
  544. netif_carrier_off(dev);
  545. priv->can.can_stats.bus_off++;
  546. /* Prevent the hardware from recovering from bus
  547. * off on its own if restart is disabled.
  548. */
  549. if (!priv->can.restart_ms)
  550. grcan_stop_hardware(dev);
  551. cf.can_id |= CAN_ERR_BUSOFF;
  552. break;
  553. case CAN_STATE_ERROR_PASSIVE:
  554. netdev_dbg(dev, "Error passive condition\n");
  555. priv->can.can_stats.error_passive++;
  556. cf.can_id |= CAN_ERR_CRTL;
  557. if (txerr >= GRCAN_STAT_ERRCNT_PASSIVE_LIMIT)
  558. cf.data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
  559. if (rxerr >= GRCAN_STAT_ERRCNT_PASSIVE_LIMIT)
  560. cf.data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
  561. break;
  562. case CAN_STATE_ERROR_WARNING:
  563. netdev_dbg(dev, "Error warning condition\n");
  564. priv->can.can_stats.error_warning++;
  565. cf.can_id |= CAN_ERR_CRTL;
  566. if (txerr >= GRCAN_STAT_ERRCNT_WARNING_LIMIT)
  567. cf.data[1] |= CAN_ERR_CRTL_TX_WARNING;
  568. if (rxerr >= GRCAN_STAT_ERRCNT_WARNING_LIMIT)
  569. cf.data[1] |= CAN_ERR_CRTL_RX_WARNING;
  570. break;
  571. case CAN_STATE_ERROR_ACTIVE:
  572. netdev_dbg(dev, "Error active condition\n");
  573. cf.can_id |= CAN_ERR_CRTL;
  574. break;
  575. default:
  576. /* There are no others at this point */
  577. break;
  578. }
  579. cf.can_id |= CAN_ERR_CNT;
  580. cf.data[6] = txerr;
  581. cf.data[7] = rxerr;
  582. priv->can.state = state;
  583. }
  584. /* Report automatic restarts */
  585. if (priv->can.restart_ms && oldstate == CAN_STATE_BUS_OFF) {
  586. unsigned long flags;
  587. cf.can_id |= CAN_ERR_RESTARTED;
  588. netdev_dbg(dev, "restarted\n");
  589. priv->can.can_stats.restarts++;
  590. netif_carrier_on(dev);
  591. spin_lock_irqsave(&priv->lock, flags);
  592. if (!priv->resetting && !priv->closing) {
  593. u32 txwr = grcan_read_reg(&regs->txwr);
  594. if (grcan_txspace(dma->tx.size, txwr,
  595. priv->eskbp))
  596. netif_wake_queue(dev);
  597. }
  598. spin_unlock_irqrestore(&priv->lock, flags);
  599. }
  600. }
  601. /* Data overrun interrupt */
  602. if ((sources & GRCAN_IRQ_OR) || (status & GRCAN_STAT_OR)) {
  603. netdev_dbg(dev, "got data overrun interrupt\n");
  604. stats->rx_over_errors++;
  605. stats->rx_errors++;
  606. cf.can_id |= CAN_ERR_CRTL;
  607. cf.data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
  608. }
  609. /* AHB bus error interrupts (not CAN bus errors) - shut down the
  610. * device.
  611. */
  612. if (sources & (GRCAN_IRQ_TXAHBERR | GRCAN_IRQ_RXAHBERR) ||
  613. (status & GRCAN_STAT_AHBERR)) {
  614. char *txrx = "";
  615. unsigned long flags;
  616. if (sources & GRCAN_IRQ_TXAHBERR) {
  617. txrx = "on tx ";
  618. stats->tx_errors++;
  619. } else if (sources & GRCAN_IRQ_RXAHBERR) {
  620. txrx = "on rx ";
  621. stats->rx_errors++;
  622. }
  623. netdev_err(dev, "Fatal AHB bus error %s- halting device\n",
  624. txrx);
  625. spin_lock_irqsave(&priv->lock, flags);
  626. /* Prevent anything to be enabled again and halt device */
  627. priv->closing = true;
  628. netif_stop_queue(dev);
  629. grcan_stop_hardware(dev);
  630. priv->can.state = CAN_STATE_STOPPED;
  631. spin_unlock_irqrestore(&priv->lock, flags);
  632. }
  633. /* Pass on error frame if something to report,
  634. * i.e. id contains some information
  635. */
  636. if (cf.can_id) {
  637. struct can_frame *skb_cf;
  638. struct sk_buff *skb = alloc_can_err_skb(dev, &skb_cf);
  639. if (skb == NULL) {
  640. netdev_dbg(dev, "could not allocate error frame\n");
  641. return;
  642. }
  643. skb_cf->can_id |= cf.can_id;
  644. memcpy(skb_cf->data, cf.data, sizeof(cf.data));
  645. netif_rx(skb);
  646. }
  647. }
  648. static irqreturn_t grcan_interrupt(int irq, void *dev_id)
  649. {
  650. struct net_device *dev = dev_id;
  651. struct grcan_priv *priv = netdev_priv(dev);
  652. struct grcan_registers __iomem *regs = priv->regs;
  653. u32 sources, status;
  654. /* Find out the source */
  655. sources = grcan_read_reg(&regs->pimsr);
  656. if (!sources)
  657. return IRQ_NONE;
  658. grcan_write_reg(&regs->picr, sources);
  659. status = grcan_read_reg(&regs->stat);
  660. /* If we got TX progress, the device has not hanged,
  661. * so disable the hang timer
  662. */
  663. if (priv->need_txbug_workaround &&
  664. (sources & (GRCAN_IRQ_TX | GRCAN_IRQ_TXLOSS))) {
  665. timer_delete(&priv->hang_timer);
  666. }
  667. /* Frame(s) received or transmitted */
  668. if (sources & (GRCAN_IRQ_TX | GRCAN_IRQ_RX)) {
  669. /* Disable tx/rx interrupts and schedule poll(). No need for
  670. * locking as interference from a running reset at worst leads
  671. * to an extra interrupt.
  672. */
  673. grcan_clear_bits(&regs->imr, GRCAN_IRQ_TX | GRCAN_IRQ_RX);
  674. napi_schedule(&priv->napi);
  675. }
  676. /* (Potential) error conditions to take care of */
  677. if (sources & GRCAN_IRQ_ERRORS)
  678. grcan_err(dev, sources, status);
  679. return IRQ_HANDLED;
  680. }
  681. /* Reset device and restart operations from where they were.
  682. *
  683. * This assumes that RXCTRL & RXCTRL is properly disabled and that RX
  684. * is not ONGOING (TX might be stuck in ONGOING due to a harwrware bug
  685. * for single shot)
  686. */
  687. static void grcan_running_reset(struct timer_list *t)
  688. {
  689. struct grcan_priv *priv = timer_container_of(priv, t, rr_timer);
  690. struct net_device *dev = priv->dev;
  691. struct grcan_registers __iomem *regs = priv->regs;
  692. unsigned long flags;
  693. /* This temporarily messes with eskbp, so we need to lock
  694. * priv->lock
  695. */
  696. spin_lock_irqsave(&priv->lock, flags);
  697. priv->resetting = false;
  698. timer_delete(&priv->hang_timer);
  699. timer_delete(&priv->rr_timer);
  700. if (!priv->closing) {
  701. /* Save and reset - config register preserved by grcan_reset */
  702. u32 imr = grcan_read_reg(&regs->imr);
  703. u32 txaddr = grcan_read_reg(&regs->txaddr);
  704. u32 txsize = grcan_read_reg(&regs->txsize);
  705. u32 txwr = grcan_read_reg(&regs->txwr);
  706. u32 txrd = grcan_read_reg(&regs->txrd);
  707. u32 eskbp = priv->eskbp;
  708. u32 rxaddr = grcan_read_reg(&regs->rxaddr);
  709. u32 rxsize = grcan_read_reg(&regs->rxsize);
  710. u32 rxwr = grcan_read_reg(&regs->rxwr);
  711. u32 rxrd = grcan_read_reg(&regs->rxrd);
  712. grcan_reset(dev);
  713. /* Restore */
  714. grcan_write_reg(&regs->txaddr, txaddr);
  715. grcan_write_reg(&regs->txsize, txsize);
  716. grcan_write_reg(&regs->txwr, txwr);
  717. grcan_write_reg(&regs->txrd, txrd);
  718. priv->eskbp = eskbp;
  719. grcan_write_reg(&regs->rxaddr, rxaddr);
  720. grcan_write_reg(&regs->rxsize, rxsize);
  721. grcan_write_reg(&regs->rxwr, rxwr);
  722. grcan_write_reg(&regs->rxrd, rxrd);
  723. /* Turn on device again */
  724. grcan_write_reg(&regs->imr, imr);
  725. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  726. grcan_write_reg(&regs->txctrl, GRCAN_TXCTRL_ENABLE
  727. | (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT
  728. ? GRCAN_TXCTRL_SINGLE : 0));
  729. grcan_write_reg(&regs->rxctrl, GRCAN_RXCTRL_ENABLE);
  730. grcan_write_reg(&regs->ctrl, GRCAN_CTRL_ENABLE);
  731. /* Start queue if there is size and listen-onle mode is not
  732. * enabled
  733. */
  734. if (grcan_txspace(priv->dma.tx.size, txwr, priv->eskbp) &&
  735. !(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
  736. netif_wake_queue(dev);
  737. }
  738. spin_unlock_irqrestore(&priv->lock, flags);
  739. netdev_err(dev, "Device reset and restored\n");
  740. }
  741. /* Waiting time in usecs corresponding to the transmission of three maximum
  742. * sized can frames in the given bitrate (in bits/sec). Waiting for this amount
  743. * of time makes sure that the can controller have time to finish sending or
  744. * receiving a frame with a good margin.
  745. *
  746. * usecs/sec * number of frames * bits/frame / bits/sec
  747. */
  748. static inline u32 grcan_ongoing_wait_usecs(__u32 bitrate)
  749. {
  750. return 1000000 * 3 * GRCAN_EFF_FRAME_MAX_BITS / bitrate;
  751. }
  752. /* Set timer so that it will not fire until after a period in which the can
  753. * controller have a good margin to finish transmitting a frame unless it has
  754. * hanged
  755. */
  756. static inline void grcan_reset_timer(struct timer_list *timer, __u32 bitrate)
  757. {
  758. u32 wait_jiffies = usecs_to_jiffies(grcan_ongoing_wait_usecs(bitrate));
  759. mod_timer(timer, jiffies + wait_jiffies);
  760. }
  761. /* Disable channels and schedule a running reset */
  762. static void grcan_initiate_running_reset(struct timer_list *t)
  763. {
  764. struct grcan_priv *priv = timer_container_of(priv, t, hang_timer);
  765. struct net_device *dev = priv->dev;
  766. struct grcan_registers __iomem *regs = priv->regs;
  767. unsigned long flags;
  768. netdev_err(dev, "Device seems hanged - reset scheduled\n");
  769. spin_lock_irqsave(&priv->lock, flags);
  770. /* The main body of this function must never be executed again
  771. * until after an execution of grcan_running_reset
  772. */
  773. if (!priv->resetting && !priv->closing) {
  774. priv->resetting = true;
  775. netif_stop_queue(dev);
  776. grcan_clear_bits(&regs->txctrl, GRCAN_TXCTRL_ENABLE);
  777. grcan_clear_bits(&regs->rxctrl, GRCAN_RXCTRL_ENABLE);
  778. grcan_reset_timer(&priv->rr_timer, priv->can.bittiming.bitrate);
  779. }
  780. spin_unlock_irqrestore(&priv->lock, flags);
  781. }
  782. static void grcan_free_dma_buffers(struct net_device *dev)
  783. {
  784. struct grcan_priv *priv = netdev_priv(dev);
  785. struct grcan_dma *dma = &priv->dma;
  786. dma_free_coherent(priv->ofdev_dev, dma->base_size, dma->base_buf,
  787. dma->base_handle);
  788. memset(dma, 0, sizeof(*dma));
  789. }
  790. static int grcan_allocate_dma_buffers(struct net_device *dev,
  791. size_t tsize, size_t rsize)
  792. {
  793. struct grcan_priv *priv = netdev_priv(dev);
  794. struct grcan_dma *dma = &priv->dma;
  795. struct grcan_dma_buffer *large = rsize > tsize ? &dma->rx : &dma->tx;
  796. struct grcan_dma_buffer *small = rsize > tsize ? &dma->tx : &dma->rx;
  797. size_t shift;
  798. /* Need a whole number of GRCAN_BUFFER_ALIGNMENT for the large,
  799. * i.e. first buffer
  800. */
  801. size_t maxs = max(tsize, rsize);
  802. size_t lsize = ALIGN(maxs, GRCAN_BUFFER_ALIGNMENT);
  803. /* Put the small buffer after that */
  804. size_t ssize = min(tsize, rsize);
  805. /* Extra GRCAN_BUFFER_ALIGNMENT to allow for alignment */
  806. dma->base_size = lsize + ssize + GRCAN_BUFFER_ALIGNMENT;
  807. dma->base_buf = dma_alloc_coherent(priv->ofdev_dev,
  808. dma->base_size,
  809. &dma->base_handle,
  810. GFP_KERNEL);
  811. if (!dma->base_buf)
  812. return -ENOMEM;
  813. dma->tx.size = tsize;
  814. dma->rx.size = rsize;
  815. large->handle = ALIGN(dma->base_handle, GRCAN_BUFFER_ALIGNMENT);
  816. small->handle = large->handle + lsize;
  817. shift = large->handle - dma->base_handle;
  818. large->buf = dma->base_buf + shift;
  819. small->buf = large->buf + lsize;
  820. return 0;
  821. }
  822. /* priv->lock *must* be held when calling this function */
  823. static int grcan_start(struct net_device *dev)
  824. {
  825. struct grcan_priv *priv = netdev_priv(dev);
  826. struct grcan_registers __iomem *regs = priv->regs;
  827. u32 confop, txctrl;
  828. grcan_reset(dev);
  829. grcan_write_reg(&regs->txaddr, priv->dma.tx.handle);
  830. grcan_write_reg(&regs->txsize, priv->dma.tx.size);
  831. /* regs->txwr, regs->txrd and priv->eskbp already set to 0 by reset */
  832. grcan_write_reg(&regs->rxaddr, priv->dma.rx.handle);
  833. grcan_write_reg(&regs->rxsize, priv->dma.rx.size);
  834. /* regs->rxwr and regs->rxrd already set to 0 by reset */
  835. /* Enable interrupts */
  836. grcan_read_reg(&regs->pir);
  837. grcan_write_reg(&regs->imr, GRCAN_IRQ_DEFAULT);
  838. /* Enable interfaces, channels and device */
  839. confop = GRCAN_CONF_ABORT
  840. | (priv->config.enable0 ? GRCAN_CONF_ENABLE0 : 0)
  841. | (priv->config.enable1 ? GRCAN_CONF_ENABLE1 : 0)
  842. | (priv->config.select ? GRCAN_CONF_SELECT : 0)
  843. | (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY ?
  844. GRCAN_CONF_SILENT : 0)
  845. | (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES ?
  846. GRCAN_CONF_SAM : 0);
  847. grcan_write_bits(&regs->conf, confop, GRCAN_CONF_OPERATION);
  848. txctrl = GRCAN_TXCTRL_ENABLE
  849. | (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT
  850. ? GRCAN_TXCTRL_SINGLE : 0);
  851. grcan_write_reg(&regs->txctrl, txctrl);
  852. grcan_write_reg(&regs->rxctrl, GRCAN_RXCTRL_ENABLE);
  853. grcan_write_reg(&regs->ctrl, GRCAN_CTRL_ENABLE);
  854. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  855. return 0;
  856. }
  857. static int grcan_set_mode(struct net_device *dev, enum can_mode mode)
  858. {
  859. struct grcan_priv *priv = netdev_priv(dev);
  860. unsigned long flags;
  861. int err = 0;
  862. if (mode == CAN_MODE_START) {
  863. /* This might be called to restart the device to recover from
  864. * bus off errors
  865. */
  866. spin_lock_irqsave(&priv->lock, flags);
  867. if (priv->closing || priv->resetting) {
  868. err = -EBUSY;
  869. } else {
  870. netdev_info(dev, "Restarting device\n");
  871. grcan_start(dev);
  872. if (!(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
  873. netif_wake_queue(dev);
  874. }
  875. spin_unlock_irqrestore(&priv->lock, flags);
  876. return err;
  877. }
  878. return -EOPNOTSUPP;
  879. }
  880. static int grcan_open(struct net_device *dev)
  881. {
  882. struct grcan_priv *priv = netdev_priv(dev);
  883. struct grcan_dma *dma = &priv->dma;
  884. unsigned long flags;
  885. int err;
  886. /* Allocate memory */
  887. err = grcan_allocate_dma_buffers(dev, priv->config.txsize,
  888. priv->config.rxsize);
  889. if (err) {
  890. netdev_err(dev, "could not allocate DMA buffers\n");
  891. return err;
  892. }
  893. priv->echo_skb = kzalloc_objs(*priv->echo_skb, dma->tx.size);
  894. if (!priv->echo_skb) {
  895. err = -ENOMEM;
  896. goto exit_free_dma_buffers;
  897. }
  898. priv->can.echo_skb_max = dma->tx.size;
  899. priv->can.echo_skb = priv->echo_skb;
  900. /* Get can device up */
  901. err = open_candev(dev);
  902. if (err)
  903. goto exit_free_echo_skb;
  904. err = request_irq(dev->irq, grcan_interrupt, IRQF_SHARED,
  905. dev->name, dev);
  906. if (err)
  907. goto exit_close_candev;
  908. napi_enable(&priv->napi);
  909. spin_lock_irqsave(&priv->lock, flags);
  910. grcan_start(dev);
  911. if (!(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
  912. netif_start_queue(dev);
  913. priv->resetting = false;
  914. priv->closing = false;
  915. spin_unlock_irqrestore(&priv->lock, flags);
  916. return 0;
  917. exit_close_candev:
  918. close_candev(dev);
  919. exit_free_echo_skb:
  920. kfree(priv->echo_skb);
  921. exit_free_dma_buffers:
  922. grcan_free_dma_buffers(dev);
  923. return err;
  924. }
  925. static int grcan_close(struct net_device *dev)
  926. {
  927. struct grcan_priv *priv = netdev_priv(dev);
  928. unsigned long flags;
  929. napi_disable(&priv->napi);
  930. spin_lock_irqsave(&priv->lock, flags);
  931. priv->closing = true;
  932. if (priv->need_txbug_workaround) {
  933. spin_unlock_irqrestore(&priv->lock, flags);
  934. timer_delete_sync(&priv->hang_timer);
  935. timer_delete_sync(&priv->rr_timer);
  936. spin_lock_irqsave(&priv->lock, flags);
  937. }
  938. netif_stop_queue(dev);
  939. grcan_stop_hardware(dev);
  940. priv->can.state = CAN_STATE_STOPPED;
  941. spin_unlock_irqrestore(&priv->lock, flags);
  942. free_irq(dev->irq, dev);
  943. close_candev(dev);
  944. grcan_free_dma_buffers(dev);
  945. priv->can.echo_skb_max = 0;
  946. priv->can.echo_skb = NULL;
  947. kfree(priv->echo_skb);
  948. return 0;
  949. }
  950. static void grcan_transmit_catch_up(struct net_device *dev)
  951. {
  952. struct grcan_priv *priv = netdev_priv(dev);
  953. unsigned long flags;
  954. int work_done;
  955. spin_lock_irqsave(&priv->lock, flags);
  956. work_done = catch_up_echo_skb(dev, -1, true);
  957. if (work_done) {
  958. if (!priv->resetting && !priv->closing &&
  959. !(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
  960. netif_wake_queue(dev);
  961. /* With napi we don't get TX interrupts for a while,
  962. * so prevent a running reset while catching up
  963. */
  964. if (priv->need_txbug_workaround)
  965. timer_delete(&priv->hang_timer);
  966. }
  967. spin_unlock_irqrestore(&priv->lock, flags);
  968. }
  969. static int grcan_receive(struct net_device *dev, int budget)
  970. {
  971. struct grcan_priv *priv = netdev_priv(dev);
  972. struct grcan_registers __iomem *regs = priv->regs;
  973. struct grcan_dma *dma = &priv->dma;
  974. struct net_device_stats *stats = &dev->stats;
  975. struct can_frame *cf;
  976. struct sk_buff *skb;
  977. u32 wr, rd, startrd;
  978. u32 *slot;
  979. u32 i, rtr, eff, j, shift;
  980. int work_done = 0;
  981. rd = grcan_read_reg(&regs->rxrd);
  982. startrd = rd;
  983. for (work_done = 0; work_done < budget; work_done++) {
  984. /* Check for packet to receive */
  985. wr = grcan_read_reg(&regs->rxwr);
  986. if (rd == wr)
  987. break;
  988. /* Take care of packet */
  989. skb = alloc_can_skb(dev, &cf);
  990. if (skb == NULL) {
  991. netdev_err(dev,
  992. "dropping frame: skb allocation failed\n");
  993. stats->rx_dropped++;
  994. continue;
  995. }
  996. slot = dma->rx.buf + rd;
  997. eff = slot[0] & GRCAN_MSG_IDE;
  998. rtr = slot[0] & GRCAN_MSG_RTR;
  999. if (eff) {
  1000. cf->can_id = ((slot[0] & GRCAN_MSG_EID)
  1001. >> GRCAN_MSG_EID_BIT);
  1002. cf->can_id |= CAN_EFF_FLAG;
  1003. } else {
  1004. cf->can_id = ((slot[0] & GRCAN_MSG_BID)
  1005. >> GRCAN_MSG_BID_BIT);
  1006. }
  1007. cf->len = can_cc_dlc2len((slot[1] & GRCAN_MSG_DLC)
  1008. >> GRCAN_MSG_DLC_BIT);
  1009. if (rtr) {
  1010. cf->can_id |= CAN_RTR_FLAG;
  1011. } else {
  1012. for (i = 0; i < cf->len; i++) {
  1013. j = GRCAN_MSG_DATA_SLOT_INDEX(i);
  1014. shift = GRCAN_MSG_DATA_SHIFT(i);
  1015. cf->data[i] = (u8)(slot[j] >> shift);
  1016. }
  1017. stats->rx_bytes += cf->len;
  1018. }
  1019. stats->rx_packets++;
  1020. netif_receive_skb(skb);
  1021. rd = grcan_ring_add(rd, GRCAN_MSG_SIZE, dma->rx.size);
  1022. }
  1023. /* Make sure everything is read before allowing hardware to
  1024. * use the memory
  1025. */
  1026. mb();
  1027. /* Update read pointer - no need to check for ongoing */
  1028. if (likely(rd != startrd))
  1029. grcan_write_reg(&regs->rxrd, rd);
  1030. return work_done;
  1031. }
  1032. static int grcan_poll(struct napi_struct *napi, int budget)
  1033. {
  1034. struct grcan_priv *priv = container_of(napi, struct grcan_priv, napi);
  1035. struct net_device *dev = priv->dev;
  1036. struct grcan_registers __iomem *regs = priv->regs;
  1037. unsigned long flags;
  1038. int work_done;
  1039. work_done = grcan_receive(dev, budget);
  1040. grcan_transmit_catch_up(dev);
  1041. if (work_done < budget) {
  1042. napi_complete(napi);
  1043. /* Guarantee no interference with a running reset that otherwise
  1044. * could turn off interrupts.
  1045. */
  1046. spin_lock_irqsave(&priv->lock, flags);
  1047. /* Enable tx and rx interrupts again. No need to check
  1048. * priv->closing as napi_disable in grcan_close is waiting for
  1049. * scheduled napi calls to finish.
  1050. */
  1051. grcan_set_bits(&regs->imr, GRCAN_IRQ_TX | GRCAN_IRQ_RX);
  1052. spin_unlock_irqrestore(&priv->lock, flags);
  1053. }
  1054. return work_done;
  1055. }
  1056. /* Work tx bug by waiting while for the risky situation to clear. If that fails,
  1057. * drop a frame in one-shot mode or indicate a busy device otherwise.
  1058. *
  1059. * Returns 0 on successful wait. Otherwise it sets *netdev_tx_status to the
  1060. * value that should be returned by grcan_start_xmit when aborting the xmit.
  1061. */
  1062. static int grcan_txbug_workaround(struct net_device *dev, struct sk_buff *skb,
  1063. u32 txwr, u32 oneshotmode,
  1064. netdev_tx_t *netdev_tx_status)
  1065. {
  1066. struct grcan_priv *priv = netdev_priv(dev);
  1067. struct grcan_registers __iomem *regs = priv->regs;
  1068. struct grcan_dma *dma = &priv->dma;
  1069. int i;
  1070. unsigned long flags;
  1071. /* Wait a while for ongoing to be cleared or read pointer to catch up to
  1072. * write pointer. The latter is needed due to a bug in older versions of
  1073. * GRCAN in which ONGOING is not cleared properly one-shot mode when a
  1074. * transmission fails.
  1075. */
  1076. for (i = 0; i < GRCAN_SHORTWAIT_USECS; i++) {
  1077. udelay(1);
  1078. if (!grcan_read_bits(&regs->txctrl, GRCAN_TXCTRL_ONGOING) ||
  1079. grcan_read_reg(&regs->txrd) == txwr) {
  1080. return 0;
  1081. }
  1082. }
  1083. /* Clean up, in case the situation was not resolved */
  1084. spin_lock_irqsave(&priv->lock, flags);
  1085. if (!priv->resetting && !priv->closing) {
  1086. /* Queue might have been stopped earlier in grcan_start_xmit */
  1087. if (grcan_txspace(dma->tx.size, txwr, priv->eskbp))
  1088. netif_wake_queue(dev);
  1089. /* Set a timer to resolve a hanged tx controller */
  1090. if (!timer_pending(&priv->hang_timer))
  1091. grcan_reset_timer(&priv->hang_timer,
  1092. priv->can.bittiming.bitrate);
  1093. }
  1094. spin_unlock_irqrestore(&priv->lock, flags);
  1095. if (oneshotmode) {
  1096. /* In one-shot mode we should never end up here because
  1097. * then the interrupt handler increases txrd on TXLOSS,
  1098. * but it is consistent with one-shot mode to drop the
  1099. * frame in this case.
  1100. */
  1101. kfree_skb(skb);
  1102. *netdev_tx_status = NETDEV_TX_OK;
  1103. } else {
  1104. /* In normal mode the socket-can transmission queue get
  1105. * to keep the frame so that it can be retransmitted
  1106. * later
  1107. */
  1108. *netdev_tx_status = NETDEV_TX_BUSY;
  1109. }
  1110. return -EBUSY;
  1111. }
  1112. /* Notes on the tx cyclic buffer handling:
  1113. *
  1114. * regs->txwr - the next slot for the driver to put data to be sent
  1115. * regs->txrd - the next slot for the device to read data
  1116. * priv->eskbp - the next slot for the driver to call can_put_echo_skb for
  1117. *
  1118. * grcan_start_xmit can enter more messages as long as regs->txwr does
  1119. * not reach priv->eskbp (within 1 message gap)
  1120. *
  1121. * The device sends messages until regs->txrd reaches regs->txwr
  1122. *
  1123. * The interrupt calls handler calls can_put_echo_skb until
  1124. * priv->eskbp reaches regs->txrd
  1125. */
  1126. static netdev_tx_t grcan_start_xmit(struct sk_buff *skb,
  1127. struct net_device *dev)
  1128. {
  1129. struct grcan_priv *priv = netdev_priv(dev);
  1130. struct grcan_registers __iomem *regs = priv->regs;
  1131. struct grcan_dma *dma = &priv->dma;
  1132. struct can_frame *cf = (struct can_frame *)skb->data;
  1133. u32 id, txwr, txrd, space, txctrl;
  1134. int slotindex;
  1135. u32 *slot;
  1136. u32 i, rtr, eff, dlc, tmp, err;
  1137. int j, shift;
  1138. unsigned long flags;
  1139. u32 oneshotmode = priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT;
  1140. if (can_dev_dropped_skb(dev, skb))
  1141. return NETDEV_TX_OK;
  1142. /* Trying to transmit in silent mode will generate error interrupts, but
  1143. * this should never happen - the queue should not have been started.
  1144. */
  1145. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  1146. return NETDEV_TX_BUSY;
  1147. /* Reads of priv->eskbp and shut-downs of the queue needs to
  1148. * be atomic towards the updates to priv->eskbp and wake-ups
  1149. * of the queue in the interrupt handler.
  1150. */
  1151. spin_lock_irqsave(&priv->lock, flags);
  1152. txwr = grcan_read_reg(&regs->txwr);
  1153. space = grcan_txspace(dma->tx.size, txwr, priv->eskbp);
  1154. slotindex = txwr / GRCAN_MSG_SIZE;
  1155. slot = dma->tx.buf + txwr;
  1156. if (unlikely(space == 1))
  1157. netif_stop_queue(dev);
  1158. spin_unlock_irqrestore(&priv->lock, flags);
  1159. /* End of critical section*/
  1160. /* This should never happen. If circular buffer is full, the
  1161. * netif_stop_queue should have been stopped already.
  1162. */
  1163. if (unlikely(!space)) {
  1164. netdev_err(dev, "No buffer space, but queue is non-stopped.\n");
  1165. return NETDEV_TX_BUSY;
  1166. }
  1167. /* Convert and write CAN message to DMA buffer */
  1168. eff = cf->can_id & CAN_EFF_FLAG;
  1169. rtr = cf->can_id & CAN_RTR_FLAG;
  1170. id = cf->can_id & (eff ? CAN_EFF_MASK : CAN_SFF_MASK);
  1171. dlc = cf->len;
  1172. if (eff)
  1173. tmp = (id << GRCAN_MSG_EID_BIT) & GRCAN_MSG_EID;
  1174. else
  1175. tmp = (id << GRCAN_MSG_BID_BIT) & GRCAN_MSG_BID;
  1176. slot[0] = (eff ? GRCAN_MSG_IDE : 0) | (rtr ? GRCAN_MSG_RTR : 0) | tmp;
  1177. slot[1] = ((dlc << GRCAN_MSG_DLC_BIT) & GRCAN_MSG_DLC);
  1178. slot[2] = 0;
  1179. slot[3] = 0;
  1180. for (i = 0; i < dlc; i++) {
  1181. j = GRCAN_MSG_DATA_SLOT_INDEX(i);
  1182. shift = GRCAN_MSG_DATA_SHIFT(i);
  1183. slot[j] |= cf->data[i] << shift;
  1184. }
  1185. /* Checking that channel has not been disabled. These cases
  1186. * should never happen
  1187. */
  1188. txctrl = grcan_read_reg(&regs->txctrl);
  1189. if (!(txctrl & GRCAN_TXCTRL_ENABLE))
  1190. netdev_err(dev, "tx channel spuriously disabled\n");
  1191. if (oneshotmode && !(txctrl & GRCAN_TXCTRL_SINGLE))
  1192. netdev_err(dev, "one-shot mode spuriously disabled\n");
  1193. /* Bug workaround for old version of grcan where updating txwr
  1194. * in the same clock cycle as the controller updates txrd to
  1195. * the current txwr could hang the can controller
  1196. */
  1197. if (priv->need_txbug_workaround) {
  1198. txrd = grcan_read_reg(&regs->txrd);
  1199. if (unlikely(grcan_ring_sub(txwr, txrd, dma->tx.size) == 1)) {
  1200. netdev_tx_t txstatus;
  1201. err = grcan_txbug_workaround(dev, skb, txwr,
  1202. oneshotmode, &txstatus);
  1203. if (err)
  1204. return txstatus;
  1205. }
  1206. }
  1207. /* Prepare skb for echoing. This must be after the bug workaround above
  1208. * as ownership of the skb is passed on by calling can_put_echo_skb.
  1209. * Returning NETDEV_TX_BUSY or accessing skb or cf after a call to
  1210. * can_put_echo_skb would be an error unless other measures are
  1211. * taken.
  1212. */
  1213. can_put_echo_skb(skb, dev, slotindex, 0);
  1214. /* Make sure everything is written before allowing hardware to
  1215. * read from the memory
  1216. */
  1217. wmb();
  1218. /* Update write pointer to start transmission */
  1219. grcan_write_reg(&regs->txwr,
  1220. grcan_ring_add(txwr, GRCAN_MSG_SIZE, dma->tx.size));
  1221. return NETDEV_TX_OK;
  1222. }
  1223. /* ========== Setting up sysfs interface and module parameters ========== */
  1224. #define GRCAN_NOT_BOOL(unsigned_val) ((unsigned_val) > 1)
  1225. #define GRCAN_MODULE_PARAM(name, mtype, valcheckf, desc) \
  1226. static void grcan_sanitize_##name(struct platform_device *pd) \
  1227. { \
  1228. struct grcan_device_config grcan_default_config \
  1229. = GRCAN_DEFAULT_DEVICE_CONFIG; \
  1230. if (valcheckf(grcan_module_config.name)) { \
  1231. dev_err(&pd->dev, \
  1232. "Invalid module parameter value for " \
  1233. #name " - setting default\n"); \
  1234. grcan_module_config.name = \
  1235. grcan_default_config.name; \
  1236. } \
  1237. } \
  1238. module_param_named(name, grcan_module_config.name, \
  1239. mtype, 0444); \
  1240. MODULE_PARM_DESC(name, desc)
  1241. #define GRCAN_CONFIG_ATTR(name, desc) \
  1242. static ssize_t grcan_store_##name(struct device *sdev, \
  1243. struct device_attribute *att, \
  1244. const char *buf, \
  1245. size_t count) \
  1246. { \
  1247. struct net_device *dev = to_net_dev(sdev); \
  1248. struct grcan_priv *priv = netdev_priv(dev); \
  1249. u8 val; \
  1250. int ret; \
  1251. if (dev->flags & IFF_UP) \
  1252. return -EBUSY; \
  1253. ret = kstrtou8(buf, 0, &val); \
  1254. if (ret < 0 || val > 1) \
  1255. return -EINVAL; \
  1256. priv->config.name = val; \
  1257. return count; \
  1258. } \
  1259. static ssize_t grcan_show_##name(struct device *sdev, \
  1260. struct device_attribute *att, \
  1261. char *buf) \
  1262. { \
  1263. struct net_device *dev = to_net_dev(sdev); \
  1264. struct grcan_priv *priv = netdev_priv(dev); \
  1265. return sprintf(buf, "%d\n", priv->config.name); \
  1266. } \
  1267. static DEVICE_ATTR(name, 0644, \
  1268. grcan_show_##name, \
  1269. grcan_store_##name); \
  1270. GRCAN_MODULE_PARAM(name, ushort, GRCAN_NOT_BOOL, desc)
  1271. /* The following configuration options are made available both via module
  1272. * parameters and writable sysfs files. See the chapter about GRCAN in the
  1273. * documentation for the GRLIB VHDL library for further details.
  1274. */
  1275. GRCAN_CONFIG_ATTR(enable0,
  1276. "Configuration of physical interface 0. Determines\n" \
  1277. "the \"Enable 0\" bit of the configuration register.\n" \
  1278. "Format: 0 | 1\nDefault: 0\n");
  1279. GRCAN_CONFIG_ATTR(enable1,
  1280. "Configuration of physical interface 1. Determines\n" \
  1281. "the \"Enable 1\" bit of the configuration register.\n" \
  1282. "Format: 0 | 1\nDefault: 0\n");
  1283. GRCAN_CONFIG_ATTR(select,
  1284. "Select which physical interface to use.\n" \
  1285. "Format: 0 | 1\nDefault: 0\n");
  1286. /* The tx and rx buffer size configuration options are only available via module
  1287. * parameters.
  1288. */
  1289. GRCAN_MODULE_PARAM(txsize, uint, GRCAN_INVALID_BUFFER_SIZE,
  1290. "Sets the size of the tx buffer.\n" \
  1291. "Format: <unsigned int> where (txsize & ~0x1fffc0) == 0\n" \
  1292. "Default: 1024\n");
  1293. GRCAN_MODULE_PARAM(rxsize, uint, GRCAN_INVALID_BUFFER_SIZE,
  1294. "Sets the size of the rx buffer.\n" \
  1295. "Format: <unsigned int> where (size & ~0x1fffc0) == 0\n" \
  1296. "Default: 1024\n");
  1297. /* Function that makes sure that configuration done using
  1298. * module parameters are set to valid values
  1299. */
  1300. static void grcan_sanitize_module_config(struct platform_device *ofdev)
  1301. {
  1302. grcan_sanitize_enable0(ofdev);
  1303. grcan_sanitize_enable1(ofdev);
  1304. grcan_sanitize_select(ofdev);
  1305. grcan_sanitize_txsize(ofdev);
  1306. grcan_sanitize_rxsize(ofdev);
  1307. }
  1308. static const struct attribute *const sysfs_grcan_attrs[] = {
  1309. /* Config attrs */
  1310. &dev_attr_enable0.attr,
  1311. &dev_attr_enable1.attr,
  1312. &dev_attr_select.attr,
  1313. NULL,
  1314. };
  1315. static const struct attribute_group sysfs_grcan_group = {
  1316. .name = "grcan",
  1317. .attrs = (struct attribute **)sysfs_grcan_attrs,
  1318. };
  1319. /* ========== Setting up the driver ========== */
  1320. static const struct net_device_ops grcan_netdev_ops = {
  1321. .ndo_open = grcan_open,
  1322. .ndo_stop = grcan_close,
  1323. .ndo_start_xmit = grcan_start_xmit,
  1324. };
  1325. static const struct ethtool_ops grcan_ethtool_ops = {
  1326. .get_ts_info = ethtool_op_get_ts_info,
  1327. };
  1328. static int grcan_setup_netdev(struct platform_device *ofdev,
  1329. void __iomem *base,
  1330. int irq, u32 ambafreq, bool txbug)
  1331. {
  1332. struct net_device *dev;
  1333. struct grcan_priv *priv;
  1334. struct grcan_registers __iomem *regs;
  1335. int err;
  1336. dev = alloc_candev(sizeof(struct grcan_priv), 0);
  1337. if (!dev)
  1338. return -ENOMEM;
  1339. dev->irq = irq;
  1340. dev->flags |= IFF_ECHO;
  1341. dev->netdev_ops = &grcan_netdev_ops;
  1342. dev->ethtool_ops = &grcan_ethtool_ops;
  1343. dev->sysfs_groups[0] = &sysfs_grcan_group;
  1344. priv = netdev_priv(dev);
  1345. memcpy(&priv->config, &grcan_module_config,
  1346. sizeof(struct grcan_device_config));
  1347. priv->dev = dev;
  1348. priv->ofdev_dev = &ofdev->dev;
  1349. priv->regs = base;
  1350. priv->can.bittiming_const = &grcan_bittiming_const;
  1351. priv->can.do_set_bittiming = grcan_set_bittiming;
  1352. priv->can.do_set_mode = grcan_set_mode;
  1353. priv->can.do_get_berr_counter = grcan_get_berr_counter;
  1354. priv->can.clock.freq = ambafreq;
  1355. priv->can.ctrlmode_supported =
  1356. CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_ONE_SHOT;
  1357. priv->need_txbug_workaround = txbug;
  1358. /* Discover if triple sampling is supported by hardware */
  1359. regs = priv->regs;
  1360. grcan_set_bits(&regs->ctrl, GRCAN_CTRL_RESET);
  1361. grcan_set_bits(&regs->conf, GRCAN_CONF_SAM);
  1362. if (grcan_read_bits(&regs->conf, GRCAN_CONF_SAM)) {
  1363. priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
  1364. dev_dbg(&ofdev->dev, "Hardware supports triple-sampling\n");
  1365. }
  1366. spin_lock_init(&priv->lock);
  1367. if (priv->need_txbug_workaround) {
  1368. timer_setup(&priv->rr_timer, grcan_running_reset, 0);
  1369. timer_setup(&priv->hang_timer, grcan_initiate_running_reset, 0);
  1370. }
  1371. netif_napi_add_weight(dev, &priv->napi, grcan_poll, GRCAN_NAPI_WEIGHT);
  1372. SET_NETDEV_DEV(dev, &ofdev->dev);
  1373. dev_info(&ofdev->dev, "regs=0x%p, irq=%d, clock=%d\n",
  1374. priv->regs, dev->irq, priv->can.clock.freq);
  1375. err = register_candev(dev);
  1376. if (err)
  1377. goto exit_free_candev;
  1378. platform_set_drvdata(ofdev, dev);
  1379. /* Reset device to allow bit-timing to be set. No need to call
  1380. * grcan_reset at this stage. That is done in grcan_open.
  1381. */
  1382. grcan_write_reg(&regs->ctrl, GRCAN_CTRL_RESET);
  1383. return 0;
  1384. exit_free_candev:
  1385. free_candev(dev);
  1386. return err;
  1387. }
  1388. static int grcan_probe(struct platform_device *ofdev)
  1389. {
  1390. struct device_node *np = ofdev->dev.of_node;
  1391. struct device_node *sysid_parent;
  1392. u32 sysid, ambafreq;
  1393. int irq, err;
  1394. void __iomem *base;
  1395. bool txbug = true;
  1396. /* Compare GRLIB version number with the first that does not
  1397. * have the tx bug (see start_xmit)
  1398. */
  1399. sysid_parent = of_find_node_by_path("/ambapp0");
  1400. if (sysid_parent) {
  1401. err = of_property_read_u32(sysid_parent, "systemid", &sysid);
  1402. if (!err && ((sysid & GRLIB_VERSION_MASK) >=
  1403. GRCAN_TXBUG_SAFE_GRLIB_VERSION))
  1404. txbug = false;
  1405. of_node_put(sysid_parent);
  1406. }
  1407. err = of_property_read_u32(np, "freq", &ambafreq);
  1408. if (err) {
  1409. dev_err(&ofdev->dev, "unable to fetch \"freq\" property\n");
  1410. goto exit_error;
  1411. }
  1412. base = devm_platform_ioremap_resource(ofdev, 0);
  1413. if (IS_ERR(base)) {
  1414. err = PTR_ERR(base);
  1415. goto exit_error;
  1416. }
  1417. irq = irq_of_parse_and_map(np, GRCAN_IRQIX_IRQ);
  1418. if (!irq) {
  1419. dev_err(&ofdev->dev, "no irq found\n");
  1420. err = -ENODEV;
  1421. goto exit_error;
  1422. }
  1423. grcan_sanitize_module_config(ofdev);
  1424. err = grcan_setup_netdev(ofdev, base, irq, ambafreq, txbug);
  1425. if (err)
  1426. goto exit_dispose_irq;
  1427. return 0;
  1428. exit_dispose_irq:
  1429. irq_dispose_mapping(irq);
  1430. exit_error:
  1431. dev_err(&ofdev->dev,
  1432. "%s socket CAN driver initialization failed with error %d\n",
  1433. DRV_NAME, err);
  1434. return err;
  1435. }
  1436. static void grcan_remove(struct platform_device *ofdev)
  1437. {
  1438. struct net_device *dev = platform_get_drvdata(ofdev);
  1439. struct grcan_priv *priv = netdev_priv(dev);
  1440. unregister_candev(dev); /* Will in turn call grcan_close */
  1441. irq_dispose_mapping(dev->irq);
  1442. netif_napi_del(&priv->napi);
  1443. free_candev(dev);
  1444. }
  1445. static const struct of_device_id grcan_match[] = {
  1446. {.name = "GAISLER_GRCAN"},
  1447. {.name = "01_03d"},
  1448. {.name = "GAISLER_GRHCAN"},
  1449. {.name = "01_034"},
  1450. {},
  1451. };
  1452. MODULE_DEVICE_TABLE(of, grcan_match);
  1453. static struct platform_driver grcan_driver = {
  1454. .driver = {
  1455. .name = DRV_NAME,
  1456. .of_match_table = grcan_match,
  1457. },
  1458. .probe = grcan_probe,
  1459. .remove = grcan_remove,
  1460. };
  1461. module_platform_driver(grcan_driver);
  1462. MODULE_AUTHOR("Aeroflex Gaisler AB.");
  1463. MODULE_DESCRIPTION("Socket CAN driver for Aeroflex Gaisler GRCAN");
  1464. MODULE_LICENSE("GPL");