grcan.c 49 KB

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