grcan.c 50 KB

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