grcan.c 50 KB

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