janz-ican3.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  1. /*
  2. * Janz MODULbus VMOD-ICAN3 CAN Interface Driver
  3. *
  4. * Copyright (c) 2010 Ira W. Snyder <iws@ovro.caltech.edu>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/delay.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/can.h>
  18. #include <linux/can/dev.h>
  19. #include <linux/can/skb.h>
  20. #include <linux/can/error.h>
  21. #include <linux/mfd/janz.h>
  22. #include <asm/io.h>
  23. /* the DPM has 64k of memory, organized into 256x 256 byte pages */
  24. #define DPM_NUM_PAGES 256
  25. #define DPM_PAGE_SIZE 256
  26. #define DPM_PAGE_ADDR(p) ((p) * DPM_PAGE_SIZE)
  27. /* JANZ ICAN3 "old-style" host interface queue page numbers */
  28. #define QUEUE_OLD_CONTROL 0
  29. #define QUEUE_OLD_RB0 1
  30. #define QUEUE_OLD_RB1 2
  31. #define QUEUE_OLD_WB0 3
  32. #define QUEUE_OLD_WB1 4
  33. /* Janz ICAN3 "old-style" host interface control registers */
  34. #define MSYNC_PEER 0x00 /* ICAN only */
  35. #define MSYNC_LOCL 0x01 /* host only */
  36. #define TARGET_RUNNING 0x02
  37. #define FIRMWARE_STAMP 0x60 /* big endian firmware stamp */
  38. #define MSYNC_RB0 0x01
  39. #define MSYNC_RB1 0x02
  40. #define MSYNC_RBLW 0x04
  41. #define MSYNC_RB_MASK (MSYNC_RB0 | MSYNC_RB1)
  42. #define MSYNC_WB0 0x10
  43. #define MSYNC_WB1 0x20
  44. #define MSYNC_WBLW 0x40
  45. #define MSYNC_WB_MASK (MSYNC_WB0 | MSYNC_WB1)
  46. /* Janz ICAN3 "new-style" host interface queue page numbers */
  47. #define QUEUE_TOHOST 5
  48. #define QUEUE_FROMHOST_MID 6
  49. #define QUEUE_FROMHOST_HIGH 7
  50. #define QUEUE_FROMHOST_LOW 8
  51. /* The first free page in the DPM is #9 */
  52. #define DPM_FREE_START 9
  53. /* Janz ICAN3 "new-style" and "fast" host interface descriptor flags */
  54. #define DESC_VALID 0x80
  55. #define DESC_WRAP 0x40
  56. #define DESC_INTERRUPT 0x20
  57. #define DESC_IVALID 0x10
  58. #define DESC_LEN(len) (len)
  59. /* Janz ICAN3 Firmware Messages */
  60. #define MSG_CONNECTI 0x02
  61. #define MSG_DISCONNECT 0x03
  62. #define MSG_IDVERS 0x04
  63. #define MSG_MSGLOST 0x05
  64. #define MSG_NEWHOSTIF 0x08
  65. #define MSG_INQUIRY 0x0a
  66. #define MSG_SETAFILMASK 0x10
  67. #define MSG_INITFDPMQUEUE 0x11
  68. #define MSG_HWCONF 0x12
  69. #define MSG_FMSGLOST 0x15
  70. #define MSG_CEVTIND 0x37
  71. #define MSG_CBTRREQ 0x41
  72. #define MSG_COFFREQ 0x42
  73. #define MSG_CONREQ 0x43
  74. #define MSG_CCONFREQ 0x47
  75. #define MSG_LMTS 0xb4
  76. /*
  77. * Janz ICAN3 CAN Inquiry Message Types
  78. *
  79. * NOTE: there appears to be a firmware bug here. You must send
  80. * NOTE: INQUIRY_STATUS and expect to receive an INQUIRY_EXTENDED
  81. * NOTE: response. The controller never responds to a message with
  82. * NOTE: the INQUIRY_EXTENDED subspec :(
  83. */
  84. #define INQUIRY_STATUS 0x00
  85. #define INQUIRY_TERMINATION 0x01
  86. #define INQUIRY_EXTENDED 0x04
  87. /* Janz ICAN3 CAN Set Acceptance Filter Mask Message Types */
  88. #define SETAFILMASK_REJECT 0x00
  89. #define SETAFILMASK_FASTIF 0x02
  90. /* Janz ICAN3 CAN Hardware Configuration Message Types */
  91. #define HWCONF_TERMINATE_ON 0x01
  92. #define HWCONF_TERMINATE_OFF 0x00
  93. /* Janz ICAN3 CAN Event Indication Message Types */
  94. #define CEVTIND_EI 0x01
  95. #define CEVTIND_DOI 0x02
  96. #define CEVTIND_LOST 0x04
  97. #define CEVTIND_FULL 0x08
  98. #define CEVTIND_BEI 0x10
  99. #define CEVTIND_CHIP_SJA1000 0x02
  100. #define ICAN3_BUSERR_QUOTA_MAX 255
  101. /* Janz ICAN3 CAN Frame Conversion */
  102. #define ICAN3_SNGL 0x02
  103. #define ICAN3_ECHO 0x10
  104. #define ICAN3_EFF_RTR 0x40
  105. #define ICAN3_SFF_RTR 0x10
  106. #define ICAN3_EFF 0x80
  107. #define ICAN3_CAN_TYPE_MASK 0x0f
  108. #define ICAN3_CAN_TYPE_SFF 0x00
  109. #define ICAN3_CAN_TYPE_EFF 0x01
  110. #define ICAN3_CAN_DLC_MASK 0x0f
  111. /*
  112. * SJA1000 Status and Error Register Definitions
  113. *
  114. * Copied from drivers/net/can/sja1000/sja1000.h
  115. */
  116. /* status register content */
  117. #define SR_BS 0x80
  118. #define SR_ES 0x40
  119. #define SR_TS 0x20
  120. #define SR_RS 0x10
  121. #define SR_TCS 0x08
  122. #define SR_TBS 0x04
  123. #define SR_DOS 0x02
  124. #define SR_RBS 0x01
  125. #define SR_CRIT (SR_BS|SR_ES)
  126. /* ECC register */
  127. #define ECC_SEG 0x1F
  128. #define ECC_DIR 0x20
  129. #define ECC_ERR 6
  130. #define ECC_BIT 0x00
  131. #define ECC_FORM 0x40
  132. #define ECC_STUFF 0x80
  133. #define ECC_MASK 0xc0
  134. /* Number of buffers for use in the "new-style" host interface */
  135. #define ICAN3_NEW_BUFFERS 16
  136. /* Number of buffers for use in the "fast" host interface */
  137. #define ICAN3_TX_BUFFERS 512
  138. #define ICAN3_RX_BUFFERS 1024
  139. /* SJA1000 Clock Input */
  140. #define ICAN3_CAN_CLOCK 8000000
  141. /* Janz ICAN3 firmware types */
  142. enum ican3_fwtype {
  143. ICAN3_FWTYPE_ICANOS,
  144. ICAN3_FWTYPE_CAL_CANOPEN,
  145. };
  146. /* Driver Name */
  147. #define DRV_NAME "janz-ican3"
  148. /* DPM Control Registers -- starts at offset 0x100 in the MODULbus registers */
  149. struct ican3_dpm_control {
  150. /* window address register */
  151. u8 window_address;
  152. u8 unused1;
  153. /*
  154. * Read access: clear interrupt from microcontroller
  155. * Write access: send interrupt to microcontroller
  156. */
  157. u8 interrupt;
  158. u8 unused2;
  159. /* write-only: reset all hardware on the module */
  160. u8 hwreset;
  161. u8 unused3;
  162. /* write-only: generate an interrupt to the TPU */
  163. u8 tpuinterrupt;
  164. };
  165. struct ican3_dev {
  166. /* must be the first member */
  167. struct can_priv can;
  168. /* CAN network device */
  169. struct net_device *ndev;
  170. struct napi_struct napi;
  171. /* module number */
  172. unsigned int num;
  173. /* base address of registers and IRQ */
  174. struct janz_cmodio_onboard_regs __iomem *ctrl;
  175. struct ican3_dpm_control __iomem *dpmctrl;
  176. void __iomem *dpm;
  177. int irq;
  178. /* CAN bus termination status */
  179. struct completion termination_comp;
  180. bool termination_enabled;
  181. /* CAN bus error status registers */
  182. struct completion buserror_comp;
  183. struct can_berr_counter bec;
  184. /* firmware type */
  185. enum ican3_fwtype fwtype;
  186. char fwinfo[32];
  187. /* old and new style host interface */
  188. unsigned int iftype;
  189. /* queue for echo packets */
  190. struct sk_buff_head echoq;
  191. /*
  192. * Any function which changes the current DPM page must hold this
  193. * lock while it is performing data accesses. This ensures that the
  194. * function will not be preempted and end up reading data from a
  195. * different DPM page than it expects.
  196. */
  197. spinlock_t lock;
  198. /* new host interface */
  199. unsigned int rx_int;
  200. unsigned int rx_num;
  201. unsigned int tx_num;
  202. /* fast host interface */
  203. unsigned int fastrx_start;
  204. unsigned int fastrx_num;
  205. unsigned int fasttx_start;
  206. unsigned int fasttx_num;
  207. /* first free DPM page */
  208. unsigned int free_page;
  209. };
  210. struct ican3_msg {
  211. u8 control;
  212. u8 spec;
  213. __le16 len;
  214. u8 data[252];
  215. };
  216. struct ican3_new_desc {
  217. u8 control;
  218. u8 pointer;
  219. };
  220. struct ican3_fast_desc {
  221. u8 control;
  222. u8 command;
  223. u8 data[14];
  224. };
  225. /* write to the window basic address register */
  226. static inline void ican3_set_page(struct ican3_dev *mod, unsigned int page)
  227. {
  228. BUG_ON(page >= DPM_NUM_PAGES);
  229. iowrite8(page, &mod->dpmctrl->window_address);
  230. }
  231. /*
  232. * ICAN3 "old-style" host interface
  233. */
  234. /*
  235. * Receive a message from the ICAN3 "old-style" firmware interface
  236. *
  237. * LOCKING: must hold mod->lock
  238. *
  239. * returns 0 on success, -ENOMEM when no message exists
  240. */
  241. static int ican3_old_recv_msg(struct ican3_dev *mod, struct ican3_msg *msg)
  242. {
  243. unsigned int mbox, mbox_page;
  244. u8 locl, peer, xord;
  245. /* get the MSYNC registers */
  246. ican3_set_page(mod, QUEUE_OLD_CONTROL);
  247. peer = ioread8(mod->dpm + MSYNC_PEER);
  248. locl = ioread8(mod->dpm + MSYNC_LOCL);
  249. xord = locl ^ peer;
  250. if ((xord & MSYNC_RB_MASK) == 0x00) {
  251. netdev_dbg(mod->ndev, "no mbox for reading\n");
  252. return -ENOMEM;
  253. }
  254. /* find the first free mbox to read */
  255. if ((xord & MSYNC_RB_MASK) == MSYNC_RB_MASK)
  256. mbox = (xord & MSYNC_RBLW) ? MSYNC_RB0 : MSYNC_RB1;
  257. else
  258. mbox = (xord & MSYNC_RB0) ? MSYNC_RB0 : MSYNC_RB1;
  259. /* copy the message */
  260. mbox_page = (mbox == MSYNC_RB0) ? QUEUE_OLD_RB0 : QUEUE_OLD_RB1;
  261. ican3_set_page(mod, mbox_page);
  262. memcpy_fromio(msg, mod->dpm, sizeof(*msg));
  263. /*
  264. * notify the firmware that the read buffer is available
  265. * for it to fill again
  266. */
  267. locl ^= mbox;
  268. ican3_set_page(mod, QUEUE_OLD_CONTROL);
  269. iowrite8(locl, mod->dpm + MSYNC_LOCL);
  270. return 0;
  271. }
  272. /*
  273. * Send a message through the "old-style" firmware interface
  274. *
  275. * LOCKING: must hold mod->lock
  276. *
  277. * returns 0 on success, -ENOMEM when no free space exists
  278. */
  279. static int ican3_old_send_msg(struct ican3_dev *mod, struct ican3_msg *msg)
  280. {
  281. unsigned int mbox, mbox_page;
  282. u8 locl, peer, xord;
  283. /* get the MSYNC registers */
  284. ican3_set_page(mod, QUEUE_OLD_CONTROL);
  285. peer = ioread8(mod->dpm + MSYNC_PEER);
  286. locl = ioread8(mod->dpm + MSYNC_LOCL);
  287. xord = locl ^ peer;
  288. if ((xord & MSYNC_WB_MASK) == MSYNC_WB_MASK) {
  289. netdev_err(mod->ndev, "no mbox for writing\n");
  290. return -ENOMEM;
  291. }
  292. /* calculate a free mbox to use */
  293. mbox = (xord & MSYNC_WB0) ? MSYNC_WB1 : MSYNC_WB0;
  294. /* copy the message to the DPM */
  295. mbox_page = (mbox == MSYNC_WB0) ? QUEUE_OLD_WB0 : QUEUE_OLD_WB1;
  296. ican3_set_page(mod, mbox_page);
  297. memcpy_toio(mod->dpm, msg, sizeof(*msg));
  298. locl ^= mbox;
  299. if (mbox == MSYNC_WB1)
  300. locl |= MSYNC_WBLW;
  301. ican3_set_page(mod, QUEUE_OLD_CONTROL);
  302. iowrite8(locl, mod->dpm + MSYNC_LOCL);
  303. return 0;
  304. }
  305. /*
  306. * ICAN3 "new-style" Host Interface Setup
  307. */
  308. static void ican3_init_new_host_interface(struct ican3_dev *mod)
  309. {
  310. struct ican3_new_desc desc;
  311. unsigned long flags;
  312. void __iomem *dst;
  313. int i;
  314. spin_lock_irqsave(&mod->lock, flags);
  315. /* setup the internal datastructures for RX */
  316. mod->rx_num = 0;
  317. mod->rx_int = 0;
  318. /* tohost queue descriptors are in page 5 */
  319. ican3_set_page(mod, QUEUE_TOHOST);
  320. dst = mod->dpm;
  321. /* initialize the tohost (rx) queue descriptors: pages 9-24 */
  322. for (i = 0; i < ICAN3_NEW_BUFFERS; i++) {
  323. desc.control = DESC_INTERRUPT | DESC_LEN(1); /* I L=1 */
  324. desc.pointer = mod->free_page;
  325. /* set wrap flag on last buffer */
  326. if (i == ICAN3_NEW_BUFFERS - 1)
  327. desc.control |= DESC_WRAP;
  328. memcpy_toio(dst, &desc, sizeof(desc));
  329. dst += sizeof(desc);
  330. mod->free_page++;
  331. }
  332. /* fromhost (tx) mid queue descriptors are in page 6 */
  333. ican3_set_page(mod, QUEUE_FROMHOST_MID);
  334. dst = mod->dpm;
  335. /* setup the internal datastructures for TX */
  336. mod->tx_num = 0;
  337. /* initialize the fromhost mid queue descriptors: pages 25-40 */
  338. for (i = 0; i < ICAN3_NEW_BUFFERS; i++) {
  339. desc.control = DESC_VALID | DESC_LEN(1); /* V L=1 */
  340. desc.pointer = mod->free_page;
  341. /* set wrap flag on last buffer */
  342. if (i == ICAN3_NEW_BUFFERS - 1)
  343. desc.control |= DESC_WRAP;
  344. memcpy_toio(dst, &desc, sizeof(desc));
  345. dst += sizeof(desc);
  346. mod->free_page++;
  347. }
  348. /* fromhost hi queue descriptors are in page 7 */
  349. ican3_set_page(mod, QUEUE_FROMHOST_HIGH);
  350. dst = mod->dpm;
  351. /* initialize only a single buffer in the fromhost hi queue (unused) */
  352. desc.control = DESC_VALID | DESC_WRAP | DESC_LEN(1); /* VW L=1 */
  353. desc.pointer = mod->free_page;
  354. memcpy_toio(dst, &desc, sizeof(desc));
  355. mod->free_page++;
  356. /* fromhost low queue descriptors are in page 8 */
  357. ican3_set_page(mod, QUEUE_FROMHOST_LOW);
  358. dst = mod->dpm;
  359. /* initialize only a single buffer in the fromhost low queue (unused) */
  360. desc.control = DESC_VALID | DESC_WRAP | DESC_LEN(1); /* VW L=1 */
  361. desc.pointer = mod->free_page;
  362. memcpy_toio(dst, &desc, sizeof(desc));
  363. mod->free_page++;
  364. spin_unlock_irqrestore(&mod->lock, flags);
  365. }
  366. /*
  367. * ICAN3 Fast Host Interface Setup
  368. */
  369. static void ican3_init_fast_host_interface(struct ican3_dev *mod)
  370. {
  371. struct ican3_fast_desc desc;
  372. unsigned long flags;
  373. unsigned int addr;
  374. void __iomem *dst;
  375. int i;
  376. spin_lock_irqsave(&mod->lock, flags);
  377. /* save the start recv page */
  378. mod->fastrx_start = mod->free_page;
  379. mod->fastrx_num = 0;
  380. /* build a single fast tohost queue descriptor */
  381. memset(&desc, 0, sizeof(desc));
  382. desc.control = 0x00;
  383. desc.command = 1;
  384. /* build the tohost queue descriptor ring in memory */
  385. addr = 0;
  386. for (i = 0; i < ICAN3_RX_BUFFERS; i++) {
  387. /* set the wrap bit on the last buffer */
  388. if (i == ICAN3_RX_BUFFERS - 1)
  389. desc.control |= DESC_WRAP;
  390. /* switch to the correct page */
  391. ican3_set_page(mod, mod->free_page);
  392. /* copy the descriptor to the DPM */
  393. dst = mod->dpm + addr;
  394. memcpy_toio(dst, &desc, sizeof(desc));
  395. addr += sizeof(desc);
  396. /* move to the next page if necessary */
  397. if (addr >= DPM_PAGE_SIZE) {
  398. addr = 0;
  399. mod->free_page++;
  400. }
  401. }
  402. /* make sure we page-align the next queue */
  403. if (addr != 0)
  404. mod->free_page++;
  405. /* save the start xmit page */
  406. mod->fasttx_start = mod->free_page;
  407. mod->fasttx_num = 0;
  408. /* build a single fast fromhost queue descriptor */
  409. memset(&desc, 0, sizeof(desc));
  410. desc.control = DESC_VALID;
  411. desc.command = 1;
  412. /* build the fromhost queue descriptor ring in memory */
  413. addr = 0;
  414. for (i = 0; i < ICAN3_TX_BUFFERS; i++) {
  415. /* set the wrap bit on the last buffer */
  416. if (i == ICAN3_TX_BUFFERS - 1)
  417. desc.control |= DESC_WRAP;
  418. /* switch to the correct page */
  419. ican3_set_page(mod, mod->free_page);
  420. /* copy the descriptor to the DPM */
  421. dst = mod->dpm + addr;
  422. memcpy_toio(dst, &desc, sizeof(desc));
  423. addr += sizeof(desc);
  424. /* move to the next page if necessary */
  425. if (addr >= DPM_PAGE_SIZE) {
  426. addr = 0;
  427. mod->free_page++;
  428. }
  429. }
  430. spin_unlock_irqrestore(&mod->lock, flags);
  431. }
  432. /*
  433. * ICAN3 "new-style" Host Interface Message Helpers
  434. */
  435. /*
  436. * LOCKING: must hold mod->lock
  437. */
  438. static int ican3_new_send_msg(struct ican3_dev *mod, struct ican3_msg *msg)
  439. {
  440. struct ican3_new_desc desc;
  441. void __iomem *desc_addr = mod->dpm + (mod->tx_num * sizeof(desc));
  442. /* switch to the fromhost mid queue, and read the buffer descriptor */
  443. ican3_set_page(mod, QUEUE_FROMHOST_MID);
  444. memcpy_fromio(&desc, desc_addr, sizeof(desc));
  445. if (!(desc.control & DESC_VALID)) {
  446. netdev_dbg(mod->ndev, "%s: no free buffers\n", __func__);
  447. return -ENOMEM;
  448. }
  449. /* switch to the data page, copy the data */
  450. ican3_set_page(mod, desc.pointer);
  451. memcpy_toio(mod->dpm, msg, sizeof(*msg));
  452. /* switch back to the descriptor, set the valid bit, write it back */
  453. ican3_set_page(mod, QUEUE_FROMHOST_MID);
  454. desc.control ^= DESC_VALID;
  455. memcpy_toio(desc_addr, &desc, sizeof(desc));
  456. /* update the tx number */
  457. mod->tx_num = (desc.control & DESC_WRAP) ? 0 : (mod->tx_num + 1);
  458. return 0;
  459. }
  460. /*
  461. * LOCKING: must hold mod->lock
  462. */
  463. static int ican3_new_recv_msg(struct ican3_dev *mod, struct ican3_msg *msg)
  464. {
  465. struct ican3_new_desc desc;
  466. void __iomem *desc_addr = mod->dpm + (mod->rx_num * sizeof(desc));
  467. /* switch to the tohost queue, and read the buffer descriptor */
  468. ican3_set_page(mod, QUEUE_TOHOST);
  469. memcpy_fromio(&desc, desc_addr, sizeof(desc));
  470. if (!(desc.control & DESC_VALID)) {
  471. netdev_dbg(mod->ndev, "%s: no buffers to recv\n", __func__);
  472. return -ENOMEM;
  473. }
  474. /* switch to the data page, copy the data */
  475. ican3_set_page(mod, desc.pointer);
  476. memcpy_fromio(msg, mod->dpm, sizeof(*msg));
  477. /* switch back to the descriptor, toggle the valid bit, write it back */
  478. ican3_set_page(mod, QUEUE_TOHOST);
  479. desc.control ^= DESC_VALID;
  480. memcpy_toio(desc_addr, &desc, sizeof(desc));
  481. /* update the rx number */
  482. mod->rx_num = (desc.control & DESC_WRAP) ? 0 : (mod->rx_num + 1);
  483. return 0;
  484. }
  485. /*
  486. * Message Send / Recv Helpers
  487. */
  488. static int ican3_send_msg(struct ican3_dev *mod, struct ican3_msg *msg)
  489. {
  490. unsigned long flags;
  491. int ret;
  492. spin_lock_irqsave(&mod->lock, flags);
  493. if (mod->iftype == 0)
  494. ret = ican3_old_send_msg(mod, msg);
  495. else
  496. ret = ican3_new_send_msg(mod, msg);
  497. spin_unlock_irqrestore(&mod->lock, flags);
  498. return ret;
  499. }
  500. static int ican3_recv_msg(struct ican3_dev *mod, struct ican3_msg *msg)
  501. {
  502. unsigned long flags;
  503. int ret;
  504. spin_lock_irqsave(&mod->lock, flags);
  505. if (mod->iftype == 0)
  506. ret = ican3_old_recv_msg(mod, msg);
  507. else
  508. ret = ican3_new_recv_msg(mod, msg);
  509. spin_unlock_irqrestore(&mod->lock, flags);
  510. return ret;
  511. }
  512. /*
  513. * Quick Pre-constructed Messages
  514. */
  515. static int ican3_msg_connect(struct ican3_dev *mod)
  516. {
  517. struct ican3_msg msg;
  518. memset(&msg, 0, sizeof(msg));
  519. msg.spec = MSG_CONNECTI;
  520. msg.len = cpu_to_le16(0);
  521. return ican3_send_msg(mod, &msg);
  522. }
  523. static int ican3_msg_disconnect(struct ican3_dev *mod)
  524. {
  525. struct ican3_msg msg;
  526. memset(&msg, 0, sizeof(msg));
  527. msg.spec = MSG_DISCONNECT;
  528. msg.len = cpu_to_le16(0);
  529. return ican3_send_msg(mod, &msg);
  530. }
  531. static int ican3_msg_newhostif(struct ican3_dev *mod)
  532. {
  533. struct ican3_msg msg;
  534. int ret;
  535. memset(&msg, 0, sizeof(msg));
  536. msg.spec = MSG_NEWHOSTIF;
  537. msg.len = cpu_to_le16(0);
  538. /* If we're not using the old interface, switching seems bogus */
  539. WARN_ON(mod->iftype != 0);
  540. ret = ican3_send_msg(mod, &msg);
  541. if (ret)
  542. return ret;
  543. /* mark the module as using the new host interface */
  544. mod->iftype = 1;
  545. return 0;
  546. }
  547. static int ican3_msg_fasthostif(struct ican3_dev *mod)
  548. {
  549. struct ican3_msg msg;
  550. unsigned int addr;
  551. memset(&msg, 0, sizeof(msg));
  552. msg.spec = MSG_INITFDPMQUEUE;
  553. msg.len = cpu_to_le16(8);
  554. /* write the tohost queue start address */
  555. addr = DPM_PAGE_ADDR(mod->fastrx_start);
  556. msg.data[0] = addr & 0xff;
  557. msg.data[1] = (addr >> 8) & 0xff;
  558. msg.data[2] = (addr >> 16) & 0xff;
  559. msg.data[3] = (addr >> 24) & 0xff;
  560. /* write the fromhost queue start address */
  561. addr = DPM_PAGE_ADDR(mod->fasttx_start);
  562. msg.data[4] = addr & 0xff;
  563. msg.data[5] = (addr >> 8) & 0xff;
  564. msg.data[6] = (addr >> 16) & 0xff;
  565. msg.data[7] = (addr >> 24) & 0xff;
  566. /* If we're not using the new interface yet, we cannot do this */
  567. WARN_ON(mod->iftype != 1);
  568. return ican3_send_msg(mod, &msg);
  569. }
  570. /*
  571. * Setup the CAN filter to either accept or reject all
  572. * messages from the CAN bus.
  573. */
  574. static int ican3_set_id_filter(struct ican3_dev *mod, bool accept)
  575. {
  576. struct ican3_msg msg;
  577. int ret;
  578. /* Standard Frame Format */
  579. memset(&msg, 0, sizeof(msg));
  580. msg.spec = MSG_SETAFILMASK;
  581. msg.len = cpu_to_le16(5);
  582. msg.data[0] = 0x00; /* IDLo LSB */
  583. msg.data[1] = 0x00; /* IDLo MSB */
  584. msg.data[2] = 0xff; /* IDHi LSB */
  585. msg.data[3] = 0x07; /* IDHi MSB */
  586. /* accept all frames for fast host if, or reject all frames */
  587. msg.data[4] = accept ? SETAFILMASK_FASTIF : SETAFILMASK_REJECT;
  588. ret = ican3_send_msg(mod, &msg);
  589. if (ret)
  590. return ret;
  591. /* Extended Frame Format */
  592. memset(&msg, 0, sizeof(msg));
  593. msg.spec = MSG_SETAFILMASK;
  594. msg.len = cpu_to_le16(13);
  595. msg.data[0] = 0; /* MUX = 0 */
  596. msg.data[1] = 0x00; /* IDLo LSB */
  597. msg.data[2] = 0x00;
  598. msg.data[3] = 0x00;
  599. msg.data[4] = 0x20; /* IDLo MSB */
  600. msg.data[5] = 0xff; /* IDHi LSB */
  601. msg.data[6] = 0xff;
  602. msg.data[7] = 0xff;
  603. msg.data[8] = 0x3f; /* IDHi MSB */
  604. /* accept all frames for fast host if, or reject all frames */
  605. msg.data[9] = accept ? SETAFILMASK_FASTIF : SETAFILMASK_REJECT;
  606. return ican3_send_msg(mod, &msg);
  607. }
  608. /*
  609. * Bring the CAN bus online or offline
  610. */
  611. static int ican3_set_bus_state(struct ican3_dev *mod, bool on)
  612. {
  613. struct can_bittiming *bt = &mod->can.bittiming;
  614. struct ican3_msg msg;
  615. u8 btr0, btr1;
  616. int res;
  617. /* This algorithm was stolen from drivers/net/can/sja1000/sja1000.c */
  618. /* The bittiming register command for the ICAN3 just sets the bit timing */
  619. /* registers on the SJA1000 chip directly */
  620. btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
  621. btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
  622. (((bt->phase_seg2 - 1) & 0x7) << 4);
  623. if (mod->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  624. btr1 |= 0x80;
  625. if (mod->fwtype == ICAN3_FWTYPE_ICANOS) {
  626. if (on) {
  627. /* set bittiming */
  628. memset(&msg, 0, sizeof(msg));
  629. msg.spec = MSG_CBTRREQ;
  630. msg.len = cpu_to_le16(4);
  631. msg.data[0] = 0x00;
  632. msg.data[1] = 0x00;
  633. msg.data[2] = btr0;
  634. msg.data[3] = btr1;
  635. res = ican3_send_msg(mod, &msg);
  636. if (res)
  637. return res;
  638. }
  639. /* can-on/off request */
  640. memset(&msg, 0, sizeof(msg));
  641. msg.spec = on ? MSG_CONREQ : MSG_COFFREQ;
  642. msg.len = cpu_to_le16(0);
  643. return ican3_send_msg(mod, &msg);
  644. } else if (mod->fwtype == ICAN3_FWTYPE_CAL_CANOPEN) {
  645. memset(&msg, 0, sizeof(msg));
  646. msg.spec = MSG_LMTS;
  647. if (on) {
  648. msg.len = cpu_to_le16(4);
  649. msg.data[0] = 0;
  650. msg.data[1] = 0;
  651. msg.data[2] = btr0;
  652. msg.data[3] = btr1;
  653. } else {
  654. msg.len = cpu_to_le16(2);
  655. msg.data[0] = 1;
  656. msg.data[1] = 0;
  657. }
  658. return ican3_send_msg(mod, &msg);
  659. }
  660. return -ENOTSUPP;
  661. }
  662. static int ican3_set_termination(struct ican3_dev *mod, bool on)
  663. {
  664. struct ican3_msg msg;
  665. memset(&msg, 0, sizeof(msg));
  666. msg.spec = MSG_HWCONF;
  667. msg.len = cpu_to_le16(2);
  668. msg.data[0] = 0x00;
  669. msg.data[1] = on ? HWCONF_TERMINATE_ON : HWCONF_TERMINATE_OFF;
  670. return ican3_send_msg(mod, &msg);
  671. }
  672. static int ican3_send_inquiry(struct ican3_dev *mod, u8 subspec)
  673. {
  674. struct ican3_msg msg;
  675. memset(&msg, 0, sizeof(msg));
  676. msg.spec = MSG_INQUIRY;
  677. msg.len = cpu_to_le16(2);
  678. msg.data[0] = subspec;
  679. msg.data[1] = 0x00;
  680. return ican3_send_msg(mod, &msg);
  681. }
  682. static int ican3_set_buserror(struct ican3_dev *mod, u8 quota)
  683. {
  684. struct ican3_msg msg;
  685. memset(&msg, 0, sizeof(msg));
  686. msg.spec = MSG_CCONFREQ;
  687. msg.len = cpu_to_le16(2);
  688. msg.data[0] = 0x00;
  689. msg.data[1] = quota;
  690. return ican3_send_msg(mod, &msg);
  691. }
  692. /*
  693. * ICAN3 to Linux CAN Frame Conversion
  694. */
  695. static void ican3_to_can_frame(struct ican3_dev *mod,
  696. struct ican3_fast_desc *desc,
  697. struct can_frame *cf)
  698. {
  699. if ((desc->command & ICAN3_CAN_TYPE_MASK) == ICAN3_CAN_TYPE_SFF) {
  700. if (desc->data[1] & ICAN3_SFF_RTR)
  701. cf->can_id |= CAN_RTR_FLAG;
  702. cf->can_id |= desc->data[0] << 3;
  703. cf->can_id |= (desc->data[1] & 0xe0) >> 5;
  704. cf->can_dlc = get_can_dlc(desc->data[1] & ICAN3_CAN_DLC_MASK);
  705. memcpy(cf->data, &desc->data[2], cf->can_dlc);
  706. } else {
  707. cf->can_dlc = get_can_dlc(desc->data[0] & ICAN3_CAN_DLC_MASK);
  708. if (desc->data[0] & ICAN3_EFF_RTR)
  709. cf->can_id |= CAN_RTR_FLAG;
  710. if (desc->data[0] & ICAN3_EFF) {
  711. cf->can_id |= CAN_EFF_FLAG;
  712. cf->can_id |= desc->data[2] << 21; /* 28-21 */
  713. cf->can_id |= desc->data[3] << 13; /* 20-13 */
  714. cf->can_id |= desc->data[4] << 5; /* 12-5 */
  715. cf->can_id |= (desc->data[5] & 0xf8) >> 3;
  716. } else {
  717. cf->can_id |= desc->data[2] << 3; /* 10-3 */
  718. cf->can_id |= desc->data[3] >> 5; /* 2-0 */
  719. }
  720. memcpy(cf->data, &desc->data[6], cf->can_dlc);
  721. }
  722. }
  723. static void can_frame_to_ican3(struct ican3_dev *mod,
  724. struct can_frame *cf,
  725. struct ican3_fast_desc *desc)
  726. {
  727. /* clear out any stale data in the descriptor */
  728. memset(desc->data, 0, sizeof(desc->data));
  729. /* we always use the extended format, with the ECHO flag set */
  730. desc->command = ICAN3_CAN_TYPE_EFF;
  731. desc->data[0] |= cf->can_dlc;
  732. desc->data[1] |= ICAN3_ECHO;
  733. /* support single transmission (no retries) mode */
  734. if (mod->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
  735. desc->data[1] |= ICAN3_SNGL;
  736. if (cf->can_id & CAN_RTR_FLAG)
  737. desc->data[0] |= ICAN3_EFF_RTR;
  738. /* pack the id into the correct places */
  739. if (cf->can_id & CAN_EFF_FLAG) {
  740. desc->data[0] |= ICAN3_EFF;
  741. desc->data[2] = (cf->can_id & 0x1fe00000) >> 21; /* 28-21 */
  742. desc->data[3] = (cf->can_id & 0x001fe000) >> 13; /* 20-13 */
  743. desc->data[4] = (cf->can_id & 0x00001fe0) >> 5; /* 12-5 */
  744. desc->data[5] = (cf->can_id & 0x0000001f) << 3; /* 4-0 */
  745. } else {
  746. desc->data[2] = (cf->can_id & 0x7F8) >> 3; /* bits 10-3 */
  747. desc->data[3] = (cf->can_id & 0x007) << 5; /* bits 2-0 */
  748. }
  749. /* copy the data bits into the descriptor */
  750. memcpy(&desc->data[6], cf->data, cf->can_dlc);
  751. }
  752. /*
  753. * Interrupt Handling
  754. */
  755. /*
  756. * Handle an ID + Version message response from the firmware. We never generate
  757. * this message in production code, but it is very useful when debugging to be
  758. * able to display this message.
  759. */
  760. static void ican3_handle_idvers(struct ican3_dev *mod, struct ican3_msg *msg)
  761. {
  762. netdev_dbg(mod->ndev, "IDVERS response: %s\n", msg->data);
  763. }
  764. static void ican3_handle_msglost(struct ican3_dev *mod, struct ican3_msg *msg)
  765. {
  766. struct net_device *dev = mod->ndev;
  767. struct net_device_stats *stats = &dev->stats;
  768. struct can_frame *cf;
  769. struct sk_buff *skb;
  770. /*
  771. * Report that communication messages with the microcontroller firmware
  772. * are being lost. These are never CAN frames, so we do not generate an
  773. * error frame for userspace
  774. */
  775. if (msg->spec == MSG_MSGLOST) {
  776. netdev_err(mod->ndev, "lost %d control messages\n", msg->data[0]);
  777. return;
  778. }
  779. /*
  780. * Oops, this indicates that we have lost messages in the fast queue,
  781. * which are exclusively CAN messages. Our driver isn't reading CAN
  782. * frames fast enough.
  783. *
  784. * We'll pretend that the SJA1000 told us that it ran out of buffer
  785. * space, because there is not a better message for this.
  786. */
  787. skb = alloc_can_err_skb(dev, &cf);
  788. if (skb) {
  789. cf->can_id |= CAN_ERR_CRTL;
  790. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  791. stats->rx_over_errors++;
  792. stats->rx_errors++;
  793. netif_rx(skb);
  794. }
  795. }
  796. /*
  797. * Handle CAN Event Indication Messages from the firmware
  798. *
  799. * The ICAN3 firmware provides the values of some SJA1000 registers when it
  800. * generates this message. The code below is largely copied from the
  801. * drivers/net/can/sja1000/sja1000.c file, and adapted as necessary
  802. */
  803. static int ican3_handle_cevtind(struct ican3_dev *mod, struct ican3_msg *msg)
  804. {
  805. struct net_device *dev = mod->ndev;
  806. struct net_device_stats *stats = &dev->stats;
  807. enum can_state state = mod->can.state;
  808. u8 isrc, ecc, status, rxerr, txerr;
  809. struct can_frame *cf;
  810. struct sk_buff *skb;
  811. /* we can only handle the SJA1000 part */
  812. if (msg->data[1] != CEVTIND_CHIP_SJA1000) {
  813. netdev_err(mod->ndev, "unable to handle errors on non-SJA1000\n");
  814. return -ENODEV;
  815. }
  816. /* check the message length for sanity */
  817. if (le16_to_cpu(msg->len) < 6) {
  818. netdev_err(mod->ndev, "error message too short\n");
  819. return -EINVAL;
  820. }
  821. isrc = msg->data[0];
  822. ecc = msg->data[2];
  823. status = msg->data[3];
  824. rxerr = msg->data[4];
  825. txerr = msg->data[5];
  826. /*
  827. * This hardware lacks any support other than bus error messages to
  828. * determine if packet transmission has failed.
  829. *
  830. * When TX errors happen, one echo skb needs to be dropped from the
  831. * front of the queue.
  832. *
  833. * A small bit of code is duplicated here and below, to avoid error
  834. * skb allocation when it will just be freed immediately.
  835. */
  836. if (isrc == CEVTIND_BEI) {
  837. int ret;
  838. netdev_dbg(mod->ndev, "bus error interrupt\n");
  839. /* TX error */
  840. if (!(ecc & ECC_DIR)) {
  841. kfree_skb(skb_dequeue(&mod->echoq));
  842. stats->tx_errors++;
  843. } else {
  844. stats->rx_errors++;
  845. }
  846. /*
  847. * The controller automatically disables bus-error interrupts
  848. * and therefore we must re-enable them.
  849. */
  850. ret = ican3_set_buserror(mod, 1);
  851. if (ret) {
  852. netdev_err(mod->ndev, "unable to re-enable bus-error\n");
  853. return ret;
  854. }
  855. /* bus error reporting is off, return immediately */
  856. if (!(mod->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
  857. return 0;
  858. }
  859. skb = alloc_can_err_skb(dev, &cf);
  860. if (skb == NULL)
  861. return -ENOMEM;
  862. /* data overrun interrupt */
  863. if (isrc == CEVTIND_DOI || isrc == CEVTIND_LOST) {
  864. netdev_dbg(mod->ndev, "data overrun interrupt\n");
  865. cf->can_id |= CAN_ERR_CRTL;
  866. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  867. stats->rx_over_errors++;
  868. stats->rx_errors++;
  869. }
  870. /* error warning + passive interrupt */
  871. if (isrc == CEVTIND_EI) {
  872. netdev_dbg(mod->ndev, "error warning + passive interrupt\n");
  873. if (status & SR_BS) {
  874. state = CAN_STATE_BUS_OFF;
  875. cf->can_id |= CAN_ERR_BUSOFF;
  876. mod->can.can_stats.bus_off++;
  877. can_bus_off(dev);
  878. } else if (status & SR_ES) {
  879. if (rxerr >= 128 || txerr >= 128)
  880. state = CAN_STATE_ERROR_PASSIVE;
  881. else
  882. state = CAN_STATE_ERROR_WARNING;
  883. } else {
  884. state = CAN_STATE_ERROR_ACTIVE;
  885. }
  886. }
  887. /* bus error interrupt */
  888. if (isrc == CEVTIND_BEI) {
  889. mod->can.can_stats.bus_error++;
  890. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  891. switch (ecc & ECC_MASK) {
  892. case ECC_BIT:
  893. cf->data[2] |= CAN_ERR_PROT_BIT;
  894. break;
  895. case ECC_FORM:
  896. cf->data[2] |= CAN_ERR_PROT_FORM;
  897. break;
  898. case ECC_STUFF:
  899. cf->data[2] |= CAN_ERR_PROT_STUFF;
  900. break;
  901. default:
  902. cf->data[3] = ecc & ECC_SEG;
  903. break;
  904. }
  905. if (!(ecc & ECC_DIR))
  906. cf->data[2] |= CAN_ERR_PROT_TX;
  907. cf->data[6] = txerr;
  908. cf->data[7] = rxerr;
  909. }
  910. if (state != mod->can.state && (state == CAN_STATE_ERROR_WARNING ||
  911. state == CAN_STATE_ERROR_PASSIVE)) {
  912. cf->can_id |= CAN_ERR_CRTL;
  913. if (state == CAN_STATE_ERROR_WARNING) {
  914. mod->can.can_stats.error_warning++;
  915. cf->data[1] = (txerr > rxerr) ?
  916. CAN_ERR_CRTL_TX_WARNING :
  917. CAN_ERR_CRTL_RX_WARNING;
  918. } else {
  919. mod->can.can_stats.error_passive++;
  920. cf->data[1] = (txerr > rxerr) ?
  921. CAN_ERR_CRTL_TX_PASSIVE :
  922. CAN_ERR_CRTL_RX_PASSIVE;
  923. }
  924. cf->data[6] = txerr;
  925. cf->data[7] = rxerr;
  926. }
  927. mod->can.state = state;
  928. netif_rx(skb);
  929. return 0;
  930. }
  931. static void ican3_handle_inquiry(struct ican3_dev *mod, struct ican3_msg *msg)
  932. {
  933. switch (msg->data[0]) {
  934. case INQUIRY_STATUS:
  935. case INQUIRY_EXTENDED:
  936. mod->bec.rxerr = msg->data[5];
  937. mod->bec.txerr = msg->data[6];
  938. complete(&mod->buserror_comp);
  939. break;
  940. case INQUIRY_TERMINATION:
  941. mod->termination_enabled = msg->data[6] & HWCONF_TERMINATE_ON;
  942. complete(&mod->termination_comp);
  943. break;
  944. default:
  945. netdev_err(mod->ndev, "received an unknown inquiry response\n");
  946. break;
  947. }
  948. }
  949. static void ican3_handle_unknown_message(struct ican3_dev *mod,
  950. struct ican3_msg *msg)
  951. {
  952. netdev_warn(mod->ndev, "received unknown message: spec 0x%.2x length %d\n",
  953. msg->spec, le16_to_cpu(msg->len));
  954. }
  955. /*
  956. * Handle a control message from the firmware
  957. */
  958. static void ican3_handle_message(struct ican3_dev *mod, struct ican3_msg *msg)
  959. {
  960. netdev_dbg(mod->ndev, "%s: modno %d spec 0x%.2x len %d bytes\n", __func__,
  961. mod->num, msg->spec, le16_to_cpu(msg->len));
  962. switch (msg->spec) {
  963. case MSG_IDVERS:
  964. ican3_handle_idvers(mod, msg);
  965. break;
  966. case MSG_MSGLOST:
  967. case MSG_FMSGLOST:
  968. ican3_handle_msglost(mod, msg);
  969. break;
  970. case MSG_CEVTIND:
  971. ican3_handle_cevtind(mod, msg);
  972. break;
  973. case MSG_INQUIRY:
  974. ican3_handle_inquiry(mod, msg);
  975. break;
  976. default:
  977. ican3_handle_unknown_message(mod, msg);
  978. break;
  979. }
  980. }
  981. /*
  982. * The ican3 needs to store all echo skbs, and therefore cannot
  983. * use the generic infrastructure for this.
  984. */
  985. static void ican3_put_echo_skb(struct ican3_dev *mod, struct sk_buff *skb)
  986. {
  987. skb = can_create_echo_skb(skb);
  988. if (!skb)
  989. return;
  990. /* save this skb for tx interrupt echo handling */
  991. skb_queue_tail(&mod->echoq, skb);
  992. }
  993. static unsigned int ican3_get_echo_skb(struct ican3_dev *mod)
  994. {
  995. struct sk_buff *skb = skb_dequeue(&mod->echoq);
  996. struct can_frame *cf;
  997. u8 dlc;
  998. /* this should never trigger unless there is a driver bug */
  999. if (!skb) {
  1000. netdev_err(mod->ndev, "BUG: echo skb not occupied\n");
  1001. return 0;
  1002. }
  1003. cf = (struct can_frame *)skb->data;
  1004. dlc = cf->can_dlc;
  1005. /* check flag whether this packet has to be looped back */
  1006. if (skb->pkt_type != PACKET_LOOPBACK) {
  1007. kfree_skb(skb);
  1008. return dlc;
  1009. }
  1010. skb->protocol = htons(ETH_P_CAN);
  1011. skb->pkt_type = PACKET_BROADCAST;
  1012. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1013. skb->dev = mod->ndev;
  1014. netif_receive_skb(skb);
  1015. return dlc;
  1016. }
  1017. /*
  1018. * Compare an skb with an existing echo skb
  1019. *
  1020. * This function will be used on devices which have a hardware loopback.
  1021. * On these devices, this function can be used to compare a received skb
  1022. * with the saved echo skbs so that the hardware echo skb can be dropped.
  1023. *
  1024. * Returns true if the skb's are identical, false otherwise.
  1025. */
  1026. static bool ican3_echo_skb_matches(struct ican3_dev *mod, struct sk_buff *skb)
  1027. {
  1028. struct can_frame *cf = (struct can_frame *)skb->data;
  1029. struct sk_buff *echo_skb = skb_peek(&mod->echoq);
  1030. struct can_frame *echo_cf;
  1031. if (!echo_skb)
  1032. return false;
  1033. echo_cf = (struct can_frame *)echo_skb->data;
  1034. if (cf->can_id != echo_cf->can_id)
  1035. return false;
  1036. if (cf->can_dlc != echo_cf->can_dlc)
  1037. return false;
  1038. return memcmp(cf->data, echo_cf->data, cf->can_dlc) == 0;
  1039. }
  1040. /*
  1041. * Check that there is room in the TX ring to transmit another skb
  1042. *
  1043. * LOCKING: must hold mod->lock
  1044. */
  1045. static bool ican3_txok(struct ican3_dev *mod)
  1046. {
  1047. struct ican3_fast_desc __iomem *desc;
  1048. u8 control;
  1049. /* check that we have echo queue space */
  1050. if (skb_queue_len(&mod->echoq) >= ICAN3_TX_BUFFERS)
  1051. return false;
  1052. /* copy the control bits of the descriptor */
  1053. ican3_set_page(mod, mod->fasttx_start + (mod->fasttx_num / 16));
  1054. desc = mod->dpm + ((mod->fasttx_num % 16) * sizeof(*desc));
  1055. control = ioread8(&desc->control);
  1056. /* if the control bits are not valid, then we have no more space */
  1057. if (!(control & DESC_VALID))
  1058. return false;
  1059. return true;
  1060. }
  1061. /*
  1062. * Receive one CAN frame from the hardware
  1063. *
  1064. * CONTEXT: must be called from user context
  1065. */
  1066. static int ican3_recv_skb(struct ican3_dev *mod)
  1067. {
  1068. struct net_device *ndev = mod->ndev;
  1069. struct net_device_stats *stats = &ndev->stats;
  1070. struct ican3_fast_desc desc;
  1071. void __iomem *desc_addr;
  1072. struct can_frame *cf;
  1073. struct sk_buff *skb;
  1074. unsigned long flags;
  1075. spin_lock_irqsave(&mod->lock, flags);
  1076. /* copy the whole descriptor */
  1077. ican3_set_page(mod, mod->fastrx_start + (mod->fastrx_num / 16));
  1078. desc_addr = mod->dpm + ((mod->fastrx_num % 16) * sizeof(desc));
  1079. memcpy_fromio(&desc, desc_addr, sizeof(desc));
  1080. spin_unlock_irqrestore(&mod->lock, flags);
  1081. /* check that we actually have a CAN frame */
  1082. if (!(desc.control & DESC_VALID))
  1083. return -ENOBUFS;
  1084. /* allocate an skb */
  1085. skb = alloc_can_skb(ndev, &cf);
  1086. if (unlikely(skb == NULL)) {
  1087. stats->rx_dropped++;
  1088. goto err_noalloc;
  1089. }
  1090. /* convert the ICAN3 frame into Linux CAN format */
  1091. ican3_to_can_frame(mod, &desc, cf);
  1092. /*
  1093. * If this is an ECHO frame received from the hardware loopback
  1094. * feature, use the skb saved in the ECHO stack instead. This allows
  1095. * the Linux CAN core to support CAN_RAW_RECV_OWN_MSGS correctly.
  1096. *
  1097. * Since this is a confirmation of a successfully transmitted packet
  1098. * sent from this host, update the transmit statistics.
  1099. *
  1100. * Also, the netdevice queue needs to be allowed to send packets again.
  1101. */
  1102. if (ican3_echo_skb_matches(mod, skb)) {
  1103. stats->tx_packets++;
  1104. stats->tx_bytes += ican3_get_echo_skb(mod);
  1105. kfree_skb(skb);
  1106. goto err_noalloc;
  1107. }
  1108. /* update statistics, receive the skb */
  1109. stats->rx_packets++;
  1110. stats->rx_bytes += cf->can_dlc;
  1111. netif_receive_skb(skb);
  1112. err_noalloc:
  1113. /* toggle the valid bit and return the descriptor to the ring */
  1114. desc.control ^= DESC_VALID;
  1115. spin_lock_irqsave(&mod->lock, flags);
  1116. ican3_set_page(mod, mod->fastrx_start + (mod->fastrx_num / 16));
  1117. memcpy_toio(desc_addr, &desc, 1);
  1118. /* update the next buffer pointer */
  1119. mod->fastrx_num = (desc.control & DESC_WRAP) ? 0
  1120. : (mod->fastrx_num + 1);
  1121. /* there are still more buffers to process */
  1122. spin_unlock_irqrestore(&mod->lock, flags);
  1123. return 0;
  1124. }
  1125. static int ican3_napi(struct napi_struct *napi, int budget)
  1126. {
  1127. struct ican3_dev *mod = container_of(napi, struct ican3_dev, napi);
  1128. unsigned long flags;
  1129. int received = 0;
  1130. int ret;
  1131. /* process all communication messages */
  1132. while (true) {
  1133. struct ican3_msg uninitialized_var(msg);
  1134. ret = ican3_recv_msg(mod, &msg);
  1135. if (ret)
  1136. break;
  1137. ican3_handle_message(mod, &msg);
  1138. }
  1139. /* process all CAN frames from the fast interface */
  1140. while (received < budget) {
  1141. ret = ican3_recv_skb(mod);
  1142. if (ret)
  1143. break;
  1144. received++;
  1145. }
  1146. /* We have processed all packets that the adapter had, but it
  1147. * was less than our budget, stop polling */
  1148. if (received < budget)
  1149. napi_complete(napi);
  1150. spin_lock_irqsave(&mod->lock, flags);
  1151. /* Wake up the transmit queue if necessary */
  1152. if (netif_queue_stopped(mod->ndev) && ican3_txok(mod))
  1153. netif_wake_queue(mod->ndev);
  1154. spin_unlock_irqrestore(&mod->lock, flags);
  1155. /* re-enable interrupt generation */
  1156. iowrite8(1 << mod->num, &mod->ctrl->int_enable);
  1157. return received;
  1158. }
  1159. static irqreturn_t ican3_irq(int irq, void *dev_id)
  1160. {
  1161. struct ican3_dev *mod = dev_id;
  1162. u8 stat;
  1163. /*
  1164. * The interrupt status register on this device reports interrupts
  1165. * as zeroes instead of using ones like most other devices
  1166. */
  1167. stat = ioread8(&mod->ctrl->int_disable) & (1 << mod->num);
  1168. if (stat == (1 << mod->num))
  1169. return IRQ_NONE;
  1170. /* clear the MODULbus interrupt from the microcontroller */
  1171. ioread8(&mod->dpmctrl->interrupt);
  1172. /* disable interrupt generation, schedule the NAPI poller */
  1173. iowrite8(1 << mod->num, &mod->ctrl->int_disable);
  1174. napi_schedule(&mod->napi);
  1175. return IRQ_HANDLED;
  1176. }
  1177. /*
  1178. * Firmware reset, startup, and shutdown
  1179. */
  1180. /*
  1181. * Reset an ICAN module to its power-on state
  1182. *
  1183. * CONTEXT: no network device registered
  1184. */
  1185. static int ican3_reset_module(struct ican3_dev *mod)
  1186. {
  1187. unsigned long start;
  1188. u8 runold, runnew;
  1189. /* disable interrupts so no more work is scheduled */
  1190. iowrite8(1 << mod->num, &mod->ctrl->int_disable);
  1191. /* the first unallocated page in the DPM is #9 */
  1192. mod->free_page = DPM_FREE_START;
  1193. ican3_set_page(mod, QUEUE_OLD_CONTROL);
  1194. runold = ioread8(mod->dpm + TARGET_RUNNING);
  1195. /* reset the module */
  1196. iowrite8(0x00, &mod->dpmctrl->hwreset);
  1197. /* wait until the module has finished resetting and is running */
  1198. start = jiffies;
  1199. do {
  1200. ican3_set_page(mod, QUEUE_OLD_CONTROL);
  1201. runnew = ioread8(mod->dpm + TARGET_RUNNING);
  1202. if (runnew == (runold ^ 0xff))
  1203. return 0;
  1204. msleep(10);
  1205. } while (time_before(jiffies, start + HZ / 2));
  1206. netdev_err(mod->ndev, "failed to reset CAN module\n");
  1207. return -ETIMEDOUT;
  1208. }
  1209. static void ican3_shutdown_module(struct ican3_dev *mod)
  1210. {
  1211. ican3_msg_disconnect(mod);
  1212. ican3_reset_module(mod);
  1213. }
  1214. /*
  1215. * Startup an ICAN module, bringing it into fast mode
  1216. */
  1217. static int ican3_startup_module(struct ican3_dev *mod)
  1218. {
  1219. int ret;
  1220. ret = ican3_reset_module(mod);
  1221. if (ret) {
  1222. netdev_err(mod->ndev, "unable to reset module\n");
  1223. return ret;
  1224. }
  1225. /* detect firmware */
  1226. memcpy_fromio(mod->fwinfo, mod->dpm + FIRMWARE_STAMP, sizeof(mod->fwinfo) - 1);
  1227. if (strncmp(mod->fwinfo, "JANZ-ICAN3", 10)) {
  1228. netdev_err(mod->ndev, "ICAN3 not detected (found %s)\n", mod->fwinfo);
  1229. return -ENODEV;
  1230. }
  1231. if (strstr(mod->fwinfo, "CAL/CANopen"))
  1232. mod->fwtype = ICAN3_FWTYPE_CAL_CANOPEN;
  1233. else
  1234. mod->fwtype = ICAN3_FWTYPE_ICANOS;
  1235. /* re-enable interrupts so we can send messages */
  1236. iowrite8(1 << mod->num, &mod->ctrl->int_enable);
  1237. ret = ican3_msg_connect(mod);
  1238. if (ret) {
  1239. netdev_err(mod->ndev, "unable to connect to module\n");
  1240. return ret;
  1241. }
  1242. ican3_init_new_host_interface(mod);
  1243. ret = ican3_msg_newhostif(mod);
  1244. if (ret) {
  1245. netdev_err(mod->ndev, "unable to switch to new-style interface\n");
  1246. return ret;
  1247. }
  1248. /* default to "termination on" */
  1249. ret = ican3_set_termination(mod, true);
  1250. if (ret) {
  1251. netdev_err(mod->ndev, "unable to enable termination\n");
  1252. return ret;
  1253. }
  1254. /* default to "bus errors enabled" */
  1255. ret = ican3_set_buserror(mod, 1);
  1256. if (ret) {
  1257. netdev_err(mod->ndev, "unable to set bus-error\n");
  1258. return ret;
  1259. }
  1260. ican3_init_fast_host_interface(mod);
  1261. ret = ican3_msg_fasthostif(mod);
  1262. if (ret) {
  1263. netdev_err(mod->ndev, "unable to switch to fast host interface\n");
  1264. return ret;
  1265. }
  1266. ret = ican3_set_id_filter(mod, true);
  1267. if (ret) {
  1268. netdev_err(mod->ndev, "unable to set acceptance filter\n");
  1269. return ret;
  1270. }
  1271. return 0;
  1272. }
  1273. /*
  1274. * CAN Network Device
  1275. */
  1276. static int ican3_open(struct net_device *ndev)
  1277. {
  1278. struct ican3_dev *mod = netdev_priv(ndev);
  1279. int ret;
  1280. /* open the CAN layer */
  1281. ret = open_candev(ndev);
  1282. if (ret) {
  1283. netdev_err(mod->ndev, "unable to start CAN layer\n");
  1284. return ret;
  1285. }
  1286. /* bring the bus online */
  1287. ret = ican3_set_bus_state(mod, true);
  1288. if (ret) {
  1289. netdev_err(mod->ndev, "unable to set bus-on\n");
  1290. close_candev(ndev);
  1291. return ret;
  1292. }
  1293. /* start up the network device */
  1294. mod->can.state = CAN_STATE_ERROR_ACTIVE;
  1295. netif_start_queue(ndev);
  1296. return 0;
  1297. }
  1298. static int ican3_stop(struct net_device *ndev)
  1299. {
  1300. struct ican3_dev *mod = netdev_priv(ndev);
  1301. int ret;
  1302. /* stop the network device xmit routine */
  1303. netif_stop_queue(ndev);
  1304. mod->can.state = CAN_STATE_STOPPED;
  1305. /* bring the bus offline, stop receiving packets */
  1306. ret = ican3_set_bus_state(mod, false);
  1307. if (ret) {
  1308. netdev_err(mod->ndev, "unable to set bus-off\n");
  1309. return ret;
  1310. }
  1311. /* drop all outstanding echo skbs */
  1312. skb_queue_purge(&mod->echoq);
  1313. /* close the CAN layer */
  1314. close_candev(ndev);
  1315. return 0;
  1316. }
  1317. static int ican3_xmit(struct sk_buff *skb, struct net_device *ndev)
  1318. {
  1319. struct ican3_dev *mod = netdev_priv(ndev);
  1320. struct can_frame *cf = (struct can_frame *)skb->data;
  1321. struct ican3_fast_desc desc;
  1322. void __iomem *desc_addr;
  1323. unsigned long flags;
  1324. if (can_dropped_invalid_skb(ndev, skb))
  1325. return NETDEV_TX_OK;
  1326. spin_lock_irqsave(&mod->lock, flags);
  1327. /* check that we can actually transmit */
  1328. if (!ican3_txok(mod)) {
  1329. netdev_err(mod->ndev, "BUG: no free descriptors\n");
  1330. spin_unlock_irqrestore(&mod->lock, flags);
  1331. return NETDEV_TX_BUSY;
  1332. }
  1333. /* copy the control bits of the descriptor */
  1334. ican3_set_page(mod, mod->fasttx_start + (mod->fasttx_num / 16));
  1335. desc_addr = mod->dpm + ((mod->fasttx_num % 16) * sizeof(desc));
  1336. memset(&desc, 0, sizeof(desc));
  1337. memcpy_fromio(&desc, desc_addr, 1);
  1338. /* convert the Linux CAN frame into ICAN3 format */
  1339. can_frame_to_ican3(mod, cf, &desc);
  1340. /*
  1341. * This hardware doesn't have TX-done notifications, so we'll try and
  1342. * emulate it the best we can using ECHO skbs. Add the skb to the ECHO
  1343. * stack. Upon packet reception, check if the ECHO skb and received
  1344. * skb match, and use that to wake the queue.
  1345. */
  1346. ican3_put_echo_skb(mod, skb);
  1347. /*
  1348. * the programming manual says that you must set the IVALID bit, then
  1349. * interrupt, then set the valid bit. Quite weird, but it seems to be
  1350. * required for this to work
  1351. */
  1352. desc.control |= DESC_IVALID;
  1353. memcpy_toio(desc_addr, &desc, sizeof(desc));
  1354. /* generate a MODULbus interrupt to the microcontroller */
  1355. iowrite8(0x01, &mod->dpmctrl->interrupt);
  1356. desc.control ^= DESC_VALID;
  1357. memcpy_toio(desc_addr, &desc, sizeof(desc));
  1358. /* update the next buffer pointer */
  1359. mod->fasttx_num = (desc.control & DESC_WRAP) ? 0
  1360. : (mod->fasttx_num + 1);
  1361. /* if there is no free descriptor space, stop the transmit queue */
  1362. if (!ican3_txok(mod))
  1363. netif_stop_queue(ndev);
  1364. spin_unlock_irqrestore(&mod->lock, flags);
  1365. return NETDEV_TX_OK;
  1366. }
  1367. static const struct net_device_ops ican3_netdev_ops = {
  1368. .ndo_open = ican3_open,
  1369. .ndo_stop = ican3_stop,
  1370. .ndo_start_xmit = ican3_xmit,
  1371. .ndo_change_mtu = can_change_mtu,
  1372. };
  1373. /*
  1374. * Low-level CAN Device
  1375. */
  1376. /* This structure was stolen from drivers/net/can/sja1000/sja1000.c */
  1377. static const struct can_bittiming_const ican3_bittiming_const = {
  1378. .name = DRV_NAME,
  1379. .tseg1_min = 1,
  1380. .tseg1_max = 16,
  1381. .tseg2_min = 1,
  1382. .tseg2_max = 8,
  1383. .sjw_max = 4,
  1384. .brp_min = 1,
  1385. .brp_max = 64,
  1386. .brp_inc = 1,
  1387. };
  1388. static int ican3_set_mode(struct net_device *ndev, enum can_mode mode)
  1389. {
  1390. struct ican3_dev *mod = netdev_priv(ndev);
  1391. int ret;
  1392. if (mode != CAN_MODE_START)
  1393. return -ENOTSUPP;
  1394. /* bring the bus online */
  1395. ret = ican3_set_bus_state(mod, true);
  1396. if (ret) {
  1397. netdev_err(ndev, "unable to set bus-on\n");
  1398. return ret;
  1399. }
  1400. /* start up the network device */
  1401. mod->can.state = CAN_STATE_ERROR_ACTIVE;
  1402. if (netif_queue_stopped(ndev))
  1403. netif_wake_queue(ndev);
  1404. return 0;
  1405. }
  1406. static int ican3_get_berr_counter(const struct net_device *ndev,
  1407. struct can_berr_counter *bec)
  1408. {
  1409. struct ican3_dev *mod = netdev_priv(ndev);
  1410. int ret;
  1411. ret = ican3_send_inquiry(mod, INQUIRY_STATUS);
  1412. if (ret)
  1413. return ret;
  1414. if (!wait_for_completion_timeout(&mod->buserror_comp, HZ)) {
  1415. netdev_info(mod->ndev, "%s timed out\n", __func__);
  1416. return -ETIMEDOUT;
  1417. }
  1418. bec->rxerr = mod->bec.rxerr;
  1419. bec->txerr = mod->bec.txerr;
  1420. return 0;
  1421. }
  1422. /*
  1423. * Sysfs Attributes
  1424. */
  1425. static ssize_t ican3_sysfs_show_term(struct device *dev,
  1426. struct device_attribute *attr,
  1427. char *buf)
  1428. {
  1429. struct ican3_dev *mod = netdev_priv(to_net_dev(dev));
  1430. int ret;
  1431. ret = ican3_send_inquiry(mod, INQUIRY_TERMINATION);
  1432. if (ret)
  1433. return ret;
  1434. if (!wait_for_completion_timeout(&mod->termination_comp, HZ)) {
  1435. netdev_info(mod->ndev, "%s timed out\n", __func__);
  1436. return -ETIMEDOUT;
  1437. }
  1438. return snprintf(buf, PAGE_SIZE, "%u\n", mod->termination_enabled);
  1439. }
  1440. static ssize_t ican3_sysfs_set_term(struct device *dev,
  1441. struct device_attribute *attr,
  1442. const char *buf, size_t count)
  1443. {
  1444. struct ican3_dev *mod = netdev_priv(to_net_dev(dev));
  1445. unsigned long enable;
  1446. int ret;
  1447. if (kstrtoul(buf, 0, &enable))
  1448. return -EINVAL;
  1449. ret = ican3_set_termination(mod, enable);
  1450. if (ret)
  1451. return ret;
  1452. return count;
  1453. }
  1454. static ssize_t ican3_sysfs_show_fwinfo(struct device *dev,
  1455. struct device_attribute *attr,
  1456. char *buf)
  1457. {
  1458. struct ican3_dev *mod = netdev_priv(to_net_dev(dev));
  1459. return scnprintf(buf, PAGE_SIZE, "%s\n", mod->fwinfo);
  1460. }
  1461. static DEVICE_ATTR(termination, S_IWUSR | S_IRUGO, ican3_sysfs_show_term,
  1462. ican3_sysfs_set_term);
  1463. static DEVICE_ATTR(fwinfo, S_IRUSR | S_IRUGO, ican3_sysfs_show_fwinfo, NULL);
  1464. static struct attribute *ican3_sysfs_attrs[] = {
  1465. &dev_attr_termination.attr,
  1466. &dev_attr_fwinfo.attr,
  1467. NULL,
  1468. };
  1469. static struct attribute_group ican3_sysfs_attr_group = {
  1470. .attrs = ican3_sysfs_attrs,
  1471. };
  1472. /*
  1473. * PCI Subsystem
  1474. */
  1475. static int ican3_probe(struct platform_device *pdev)
  1476. {
  1477. struct janz_platform_data *pdata;
  1478. struct net_device *ndev;
  1479. struct ican3_dev *mod;
  1480. struct resource *res;
  1481. struct device *dev;
  1482. int ret;
  1483. pdata = dev_get_platdata(&pdev->dev);
  1484. if (!pdata)
  1485. return -ENXIO;
  1486. dev_dbg(&pdev->dev, "probe: module number %d\n", pdata->modno);
  1487. /* save the struct device for printing */
  1488. dev = &pdev->dev;
  1489. /* allocate the CAN device and private data */
  1490. ndev = alloc_candev(sizeof(*mod), 0);
  1491. if (!ndev) {
  1492. dev_err(dev, "unable to allocate CANdev\n");
  1493. ret = -ENOMEM;
  1494. goto out_return;
  1495. }
  1496. platform_set_drvdata(pdev, ndev);
  1497. mod = netdev_priv(ndev);
  1498. mod->ndev = ndev;
  1499. mod->num = pdata->modno;
  1500. netif_napi_add(ndev, &mod->napi, ican3_napi, ICAN3_RX_BUFFERS);
  1501. skb_queue_head_init(&mod->echoq);
  1502. spin_lock_init(&mod->lock);
  1503. init_completion(&mod->termination_comp);
  1504. init_completion(&mod->buserror_comp);
  1505. /* setup device-specific sysfs attributes */
  1506. ndev->sysfs_groups[0] = &ican3_sysfs_attr_group;
  1507. /* the first unallocated page in the DPM is 9 */
  1508. mod->free_page = DPM_FREE_START;
  1509. ndev->netdev_ops = &ican3_netdev_ops;
  1510. ndev->flags |= IFF_ECHO;
  1511. SET_NETDEV_DEV(ndev, &pdev->dev);
  1512. mod->can.clock.freq = ICAN3_CAN_CLOCK;
  1513. mod->can.bittiming_const = &ican3_bittiming_const;
  1514. mod->can.do_set_mode = ican3_set_mode;
  1515. mod->can.do_get_berr_counter = ican3_get_berr_counter;
  1516. mod->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES
  1517. | CAN_CTRLMODE_BERR_REPORTING
  1518. | CAN_CTRLMODE_ONE_SHOT;
  1519. /* find our IRQ number */
  1520. mod->irq = platform_get_irq(pdev, 0);
  1521. if (mod->irq < 0) {
  1522. dev_err(dev, "IRQ line not found\n");
  1523. ret = -ENODEV;
  1524. goto out_free_ndev;
  1525. }
  1526. ndev->irq = mod->irq;
  1527. /* get access to the MODULbus registers for this module */
  1528. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1529. if (!res) {
  1530. dev_err(dev, "MODULbus registers not found\n");
  1531. ret = -ENODEV;
  1532. goto out_free_ndev;
  1533. }
  1534. mod->dpm = ioremap(res->start, resource_size(res));
  1535. if (!mod->dpm) {
  1536. dev_err(dev, "MODULbus registers not ioremap\n");
  1537. ret = -ENOMEM;
  1538. goto out_free_ndev;
  1539. }
  1540. mod->dpmctrl = mod->dpm + DPM_PAGE_SIZE;
  1541. /* get access to the control registers for this module */
  1542. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1543. if (!res) {
  1544. dev_err(dev, "CONTROL registers not found\n");
  1545. ret = -ENODEV;
  1546. goto out_iounmap_dpm;
  1547. }
  1548. mod->ctrl = ioremap(res->start, resource_size(res));
  1549. if (!mod->ctrl) {
  1550. dev_err(dev, "CONTROL registers not ioremap\n");
  1551. ret = -ENOMEM;
  1552. goto out_iounmap_dpm;
  1553. }
  1554. /* disable our IRQ, then hookup the IRQ handler */
  1555. iowrite8(1 << mod->num, &mod->ctrl->int_disable);
  1556. ret = request_irq(mod->irq, ican3_irq, IRQF_SHARED, DRV_NAME, mod);
  1557. if (ret) {
  1558. dev_err(dev, "unable to request IRQ\n");
  1559. goto out_iounmap_ctrl;
  1560. }
  1561. /* reset and initialize the CAN controller into fast mode */
  1562. napi_enable(&mod->napi);
  1563. ret = ican3_startup_module(mod);
  1564. if (ret) {
  1565. dev_err(dev, "%s: unable to start CANdev\n", __func__);
  1566. goto out_free_irq;
  1567. }
  1568. /* register with the Linux CAN layer */
  1569. ret = register_candev(ndev);
  1570. if (ret) {
  1571. dev_err(dev, "%s: unable to register CANdev\n", __func__);
  1572. goto out_free_irq;
  1573. }
  1574. netdev_info(mod->ndev, "module %d: registered CAN device\n", pdata->modno);
  1575. return 0;
  1576. out_free_irq:
  1577. napi_disable(&mod->napi);
  1578. iowrite8(1 << mod->num, &mod->ctrl->int_disable);
  1579. free_irq(mod->irq, mod);
  1580. out_iounmap_ctrl:
  1581. iounmap(mod->ctrl);
  1582. out_iounmap_dpm:
  1583. iounmap(mod->dpm);
  1584. out_free_ndev:
  1585. free_candev(ndev);
  1586. out_return:
  1587. return ret;
  1588. }
  1589. static int ican3_remove(struct platform_device *pdev)
  1590. {
  1591. struct net_device *ndev = platform_get_drvdata(pdev);
  1592. struct ican3_dev *mod = netdev_priv(ndev);
  1593. /* unregister the netdevice, stop interrupts */
  1594. unregister_netdev(ndev);
  1595. napi_disable(&mod->napi);
  1596. iowrite8(1 << mod->num, &mod->ctrl->int_disable);
  1597. free_irq(mod->irq, mod);
  1598. /* put the module into reset */
  1599. ican3_shutdown_module(mod);
  1600. /* unmap all registers */
  1601. iounmap(mod->ctrl);
  1602. iounmap(mod->dpm);
  1603. free_candev(ndev);
  1604. return 0;
  1605. }
  1606. static struct platform_driver ican3_driver = {
  1607. .driver = {
  1608. .name = DRV_NAME,
  1609. },
  1610. .probe = ican3_probe,
  1611. .remove = ican3_remove,
  1612. };
  1613. module_platform_driver(ican3_driver);
  1614. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  1615. MODULE_DESCRIPTION("Janz MODULbus VMOD-ICAN3 Driver");
  1616. MODULE_LICENSE("GPL");
  1617. MODULE_ALIAS("platform:janz-ican3");