grcan.c 49 KB

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