janz-ican3.c 43 KB

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