xsysace.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. /*
  2. * Xilinx SystemACE device driver
  3. *
  4. * Copyright 2007 Secret Lab Technologies Ltd.
  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 version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. /*
  11. * The SystemACE chip is designed to configure FPGAs by loading an FPGA
  12. * bitstream from a file on a CF card and squirting it into FPGAs connected
  13. * to the SystemACE JTAG chain. It also has the advantage of providing an
  14. * MPU interface which can be used to control the FPGA configuration process
  15. * and to use the attached CF card for general purpose storage.
  16. *
  17. * This driver is a block device driver for the SystemACE.
  18. *
  19. * Initialization:
  20. * The driver registers itself as a platform_device driver at module
  21. * load time. The platform bus will take care of calling the
  22. * ace_probe() method for all SystemACE instances in the system. Any
  23. * number of SystemACE instances are supported. ace_probe() calls
  24. * ace_setup() which initialized all data structures, reads the CF
  25. * id structure and registers the device.
  26. *
  27. * Processing:
  28. * Just about all of the heavy lifting in this driver is performed by
  29. * a Finite State Machine (FSM). The driver needs to wait on a number
  30. * of events; some raised by interrupts, some which need to be polled
  31. * for. Describing all of the behaviour in a FSM seems to be the
  32. * easiest way to keep the complexity low and make it easy to
  33. * understand what the driver is doing. If the block ops or the
  34. * request function need to interact with the hardware, then they
  35. * simply need to flag the request and kick of FSM processing.
  36. *
  37. * The FSM itself is atomic-safe code which can be run from any
  38. * context. The general process flow is:
  39. * 1. obtain the ace->lock spinlock.
  40. * 2. loop on ace_fsm_dostate() until the ace->fsm_continue flag is
  41. * cleared.
  42. * 3. release the lock.
  43. *
  44. * Individual states do not sleep in any way. If a condition needs to
  45. * be waited for then the state much clear the fsm_continue flag and
  46. * either schedule the FSM to be run again at a later time, or expect
  47. * an interrupt to call the FSM when the desired condition is met.
  48. *
  49. * In normal operation, the FSM is processed at interrupt context
  50. * either when the driver's tasklet is scheduled, or when an irq is
  51. * raised by the hardware. The tasklet can be scheduled at any time.
  52. * The request method in particular schedules the tasklet when a new
  53. * request has been indicated by the block layer. Once started, the
  54. * FSM proceeds as far as it can processing the request until it
  55. * needs on a hardware event. At this point, it must yield execution.
  56. *
  57. * A state has two options when yielding execution:
  58. * 1. ace_fsm_yield()
  59. * - Call if need to poll for event.
  60. * - clears the fsm_continue flag to exit the processing loop
  61. * - reschedules the tasklet to run again as soon as possible
  62. * 2. ace_fsm_yieldirq()
  63. * - Call if an irq is expected from the HW
  64. * - clears the fsm_continue flag to exit the processing loop
  65. * - does not reschedule the tasklet so the FSM will not be processed
  66. * again until an irq is received.
  67. * After calling a yield function, the state must return control back
  68. * to the FSM main loop.
  69. *
  70. * Additionally, the driver maintains a kernel timer which can process
  71. * the FSM. If the FSM gets stalled, typically due to a missed
  72. * interrupt, then the kernel timer will expire and the driver can
  73. * continue where it left off.
  74. *
  75. * To Do:
  76. * - Add FPGA configuration control interface.
  77. * - Request major number from lanana
  78. */
  79. #undef DEBUG
  80. #include <linux/module.h>
  81. #include <linux/ctype.h>
  82. #include <linux/init.h>
  83. #include <linux/interrupt.h>
  84. #include <linux/errno.h>
  85. #include <linux/kernel.h>
  86. #include <linux/delay.h>
  87. #include <linux/slab.h>
  88. #include <linux/blkdev.h>
  89. #include <linux/hdreg.h>
  90. #include <linux/platform_device.h>
  91. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  92. MODULE_DESCRIPTION("Xilinx SystemACE device driver");
  93. MODULE_LICENSE("GPL");
  94. /* SystemACE register definitions */
  95. #define ACE_BUSMODE (0x00)
  96. #define ACE_STATUS (0x04)
  97. #define ACE_STATUS_CFGLOCK (0x00000001)
  98. #define ACE_STATUS_MPULOCK (0x00000002)
  99. #define ACE_STATUS_CFGERROR (0x00000004) /* config controller error */
  100. #define ACE_STATUS_CFCERROR (0x00000008) /* CF controller error */
  101. #define ACE_STATUS_CFDETECT (0x00000010)
  102. #define ACE_STATUS_DATABUFRDY (0x00000020)
  103. #define ACE_STATUS_DATABUFMODE (0x00000040)
  104. #define ACE_STATUS_CFGDONE (0x00000080)
  105. #define ACE_STATUS_RDYFORCFCMD (0x00000100)
  106. #define ACE_STATUS_CFGMODEPIN (0x00000200)
  107. #define ACE_STATUS_CFGADDR_MASK (0x0000e000)
  108. #define ACE_STATUS_CFBSY (0x00020000)
  109. #define ACE_STATUS_CFRDY (0x00040000)
  110. #define ACE_STATUS_CFDWF (0x00080000)
  111. #define ACE_STATUS_CFDSC (0x00100000)
  112. #define ACE_STATUS_CFDRQ (0x00200000)
  113. #define ACE_STATUS_CFCORR (0x00400000)
  114. #define ACE_STATUS_CFERR (0x00800000)
  115. #define ACE_ERROR (0x08)
  116. #define ACE_CFGLBA (0x0c)
  117. #define ACE_MPULBA (0x10)
  118. #define ACE_SECCNTCMD (0x14)
  119. #define ACE_SECCNTCMD_RESET (0x0100)
  120. #define ACE_SECCNTCMD_IDENTIFY (0x0200)
  121. #define ACE_SECCNTCMD_READ_DATA (0x0300)
  122. #define ACE_SECCNTCMD_WRITE_DATA (0x0400)
  123. #define ACE_SECCNTCMD_ABORT (0x0600)
  124. #define ACE_VERSION (0x16)
  125. #define ACE_VERSION_REVISION_MASK (0x00FF)
  126. #define ACE_VERSION_MINOR_MASK (0x0F00)
  127. #define ACE_VERSION_MAJOR_MASK (0xF000)
  128. #define ACE_CTRL (0x18)
  129. #define ACE_CTRL_FORCELOCKREQ (0x0001)
  130. #define ACE_CTRL_LOCKREQ (0x0002)
  131. #define ACE_CTRL_FORCECFGADDR (0x0004)
  132. #define ACE_CTRL_FORCECFGMODE (0x0008)
  133. #define ACE_CTRL_CFGMODE (0x0010)
  134. #define ACE_CTRL_CFGSTART (0x0020)
  135. #define ACE_CTRL_CFGSEL (0x0040)
  136. #define ACE_CTRL_CFGRESET (0x0080)
  137. #define ACE_CTRL_DATABUFRDYIRQ (0x0100)
  138. #define ACE_CTRL_ERRORIRQ (0x0200)
  139. #define ACE_CTRL_CFGDONEIRQ (0x0400)
  140. #define ACE_CTRL_RESETIRQ (0x0800)
  141. #define ACE_CTRL_CFGPROG (0x1000)
  142. #define ACE_CTRL_CFGADDR_MASK (0xe000)
  143. #define ACE_FATSTAT (0x1c)
  144. #define ACE_NUM_MINORS 16
  145. #define ACE_SECTOR_SIZE (512)
  146. #define ACE_FIFO_SIZE (32)
  147. #define ACE_BUF_PER_SECTOR (ACE_SECTOR_SIZE / ACE_FIFO_SIZE)
  148. struct ace_reg_ops;
  149. struct ace_device {
  150. /* driver state data */
  151. int id;
  152. int media_change;
  153. int users;
  154. struct list_head list;
  155. /* finite state machine data */
  156. struct tasklet_struct fsm_tasklet;
  157. uint fsm_task; /* Current activity (ACE_TASK_*) */
  158. uint fsm_state; /* Current state (ACE_FSM_STATE_*) */
  159. uint fsm_continue_flag; /* cleared to exit FSM mainloop */
  160. uint fsm_iter_num;
  161. struct timer_list stall_timer;
  162. /* Transfer state/result, use for both id and block request */
  163. struct request *req; /* request being processed */
  164. void *data_ptr; /* pointer to I/O buffer */
  165. int data_count; /* number of buffers remaining */
  166. int data_result; /* Result of transfer; 0 := success */
  167. int id_req_count; /* count of id requests */
  168. int id_result;
  169. struct completion id_completion; /* used when id req finishes */
  170. int in_irq;
  171. /* Details of hardware device */
  172. unsigned long physaddr;
  173. void *baseaddr;
  174. int irq;
  175. int bus_width; /* 0 := 8 bit; 1 := 16 bit */
  176. struct ace_reg_ops *reg_ops;
  177. int lock_count;
  178. /* Block device data structures */
  179. spinlock_t lock;
  180. struct device *dev;
  181. struct request_queue *queue;
  182. struct gendisk *gd;
  183. /* Inserted CF card parameters */
  184. struct hd_driveid cf_id;
  185. };
  186. static int ace_major;
  187. /* ---------------------------------------------------------------------
  188. * Low level register access
  189. */
  190. struct ace_reg_ops {
  191. u16(*in) (struct ace_device * ace, int reg);
  192. void (*out) (struct ace_device * ace, int reg, u16 val);
  193. void (*datain) (struct ace_device * ace);
  194. void (*dataout) (struct ace_device * ace);
  195. };
  196. /* 8 Bit bus width */
  197. static u16 ace_in_8(struct ace_device *ace, int reg)
  198. {
  199. void *r = ace->baseaddr + reg;
  200. return in_8(r) | (in_8(r + 1) << 8);
  201. }
  202. static void ace_out_8(struct ace_device *ace, int reg, u16 val)
  203. {
  204. void *r = ace->baseaddr + reg;
  205. out_8(r, val);
  206. out_8(r + 1, val >> 8);
  207. }
  208. static void ace_datain_8(struct ace_device *ace)
  209. {
  210. void *r = ace->baseaddr + 0x40;
  211. u8 *dst = ace->data_ptr;
  212. int i = ACE_FIFO_SIZE;
  213. while (i--)
  214. *dst++ = in_8(r++);
  215. ace->data_ptr = dst;
  216. }
  217. static void ace_dataout_8(struct ace_device *ace)
  218. {
  219. void *r = ace->baseaddr + 0x40;
  220. u8 *src = ace->data_ptr;
  221. int i = ACE_FIFO_SIZE;
  222. while (i--)
  223. out_8(r++, *src++);
  224. ace->data_ptr = src;
  225. }
  226. static struct ace_reg_ops ace_reg_8_ops = {
  227. .in = ace_in_8,
  228. .out = ace_out_8,
  229. .datain = ace_datain_8,
  230. .dataout = ace_dataout_8,
  231. };
  232. /* 16 bit big endian bus attachment */
  233. static u16 ace_in_be16(struct ace_device *ace, int reg)
  234. {
  235. return in_be16(ace->baseaddr + reg);
  236. }
  237. static void ace_out_be16(struct ace_device *ace, int reg, u16 val)
  238. {
  239. out_be16(ace->baseaddr + reg, val);
  240. }
  241. static void ace_datain_be16(struct ace_device *ace)
  242. {
  243. int i = ACE_FIFO_SIZE / 2;
  244. u16 *dst = ace->data_ptr;
  245. while (i--)
  246. *dst++ = in_le16(ace->baseaddr + 0x40);
  247. ace->data_ptr = dst;
  248. }
  249. static void ace_dataout_be16(struct ace_device *ace)
  250. {
  251. int i = ACE_FIFO_SIZE / 2;
  252. u16 *src = ace->data_ptr;
  253. while (i--)
  254. out_le16(ace->baseaddr + 0x40, *src++);
  255. ace->data_ptr = src;
  256. }
  257. /* 16 bit little endian bus attachment */
  258. static u16 ace_in_le16(struct ace_device *ace, int reg)
  259. {
  260. return in_le16(ace->baseaddr + reg);
  261. }
  262. static void ace_out_le16(struct ace_device *ace, int reg, u16 val)
  263. {
  264. out_le16(ace->baseaddr + reg, val);
  265. }
  266. static void ace_datain_le16(struct ace_device *ace)
  267. {
  268. int i = ACE_FIFO_SIZE / 2;
  269. u16 *dst = ace->data_ptr;
  270. while (i--)
  271. *dst++ = in_be16(ace->baseaddr + 0x40);
  272. ace->data_ptr = dst;
  273. }
  274. static void ace_dataout_le16(struct ace_device *ace)
  275. {
  276. int i = ACE_FIFO_SIZE / 2;
  277. u16 *src = ace->data_ptr;
  278. while (i--)
  279. out_be16(ace->baseaddr + 0x40, *src++);
  280. ace->data_ptr = src;
  281. }
  282. static struct ace_reg_ops ace_reg_be16_ops = {
  283. .in = ace_in_be16,
  284. .out = ace_out_be16,
  285. .datain = ace_datain_be16,
  286. .dataout = ace_dataout_be16,
  287. };
  288. static struct ace_reg_ops ace_reg_le16_ops = {
  289. .in = ace_in_le16,
  290. .out = ace_out_le16,
  291. .datain = ace_datain_le16,
  292. .dataout = ace_dataout_le16,
  293. };
  294. static inline u16 ace_in(struct ace_device *ace, int reg)
  295. {
  296. return ace->reg_ops->in(ace, reg);
  297. }
  298. static inline u32 ace_in32(struct ace_device *ace, int reg)
  299. {
  300. return ace_in(ace, reg) | (ace_in(ace, reg + 2) << 16);
  301. }
  302. static inline void ace_out(struct ace_device *ace, int reg, u16 val)
  303. {
  304. ace->reg_ops->out(ace, reg, val);
  305. }
  306. static inline void ace_out32(struct ace_device *ace, int reg, u32 val)
  307. {
  308. ace_out(ace, reg, val);
  309. ace_out(ace, reg + 2, val >> 16);
  310. }
  311. /* ---------------------------------------------------------------------
  312. * Debug support functions
  313. */
  314. #if defined(DEBUG)
  315. static void ace_dump_mem(void *base, int len)
  316. {
  317. const char *ptr = base;
  318. int i, j;
  319. for (i = 0; i < len; i += 16) {
  320. printk(KERN_INFO "%.8x:", i);
  321. for (j = 0; j < 16; j++) {
  322. if (!(j % 4))
  323. printk(" ");
  324. printk("%.2x", ptr[i + j]);
  325. }
  326. printk(" ");
  327. for (j = 0; j < 16; j++)
  328. printk("%c", isprint(ptr[i + j]) ? ptr[i + j] : '.');
  329. printk("\n");
  330. }
  331. }
  332. #else
  333. static inline void ace_dump_mem(void *base, int len)
  334. {
  335. }
  336. #endif
  337. static void ace_dump_regs(struct ace_device *ace)
  338. {
  339. dev_info(ace->dev, " ctrl: %.8x seccnt/cmd: %.4x ver:%.4x\n"
  340. " status:%.8x mpu_lba:%.8x busmode:%4x\n"
  341. " error: %.8x cfg_lba:%.8x fatstat:%.4x\n",
  342. ace_in32(ace, ACE_CTRL),
  343. ace_in(ace, ACE_SECCNTCMD),
  344. ace_in(ace, ACE_VERSION),
  345. ace_in32(ace, ACE_STATUS),
  346. ace_in32(ace, ACE_MPULBA),
  347. ace_in(ace, ACE_BUSMODE),
  348. ace_in32(ace, ACE_ERROR),
  349. ace_in32(ace, ACE_CFGLBA), ace_in(ace, ACE_FATSTAT));
  350. }
  351. void ace_fix_driveid(struct hd_driveid *id)
  352. {
  353. #if defined(__BIG_ENDIAN)
  354. u16 *buf = (void *)id;
  355. int i;
  356. /* All half words have wrong byte order; swap the bytes */
  357. for (i = 0; i < sizeof(struct hd_driveid); i += 2, buf++)
  358. *buf = le16_to_cpu(*buf);
  359. /* Some of the data values are 32bit; swap the half words */
  360. id->lba_capacity = ((id->lba_capacity >> 16) & 0x0000FFFF) |
  361. ((id->lba_capacity << 16) & 0xFFFF0000);
  362. id->spg = ((id->spg >> 16) & 0x0000FFFF) |
  363. ((id->spg << 16) & 0xFFFF0000);
  364. #endif
  365. }
  366. /* ---------------------------------------------------------------------
  367. * Finite State Machine (FSM) implementation
  368. */
  369. /* FSM tasks; used to direct state transitions */
  370. #define ACE_TASK_IDLE 0
  371. #define ACE_TASK_IDENTIFY 1
  372. #define ACE_TASK_READ 2
  373. #define ACE_TASK_WRITE 3
  374. #define ACE_FSM_NUM_TASKS 4
  375. /* FSM state definitions */
  376. #define ACE_FSM_STATE_IDLE 0
  377. #define ACE_FSM_STATE_REQ_LOCK 1
  378. #define ACE_FSM_STATE_WAIT_LOCK 2
  379. #define ACE_FSM_STATE_WAIT_CFREADY 3
  380. #define ACE_FSM_STATE_IDENTIFY_PREPARE 4
  381. #define ACE_FSM_STATE_IDENTIFY_TRANSFER 5
  382. #define ACE_FSM_STATE_IDENTIFY_COMPLETE 6
  383. #define ACE_FSM_STATE_REQ_PREPARE 7
  384. #define ACE_FSM_STATE_REQ_TRANSFER 8
  385. #define ACE_FSM_STATE_REQ_COMPLETE 9
  386. #define ACE_FSM_STATE_ERROR 10
  387. #define ACE_FSM_NUM_STATES 11
  388. /* Set flag to exit FSM loop and reschedule tasklet */
  389. static inline void ace_fsm_yield(struct ace_device *ace)
  390. {
  391. dev_dbg(ace->dev, "ace_fsm_yield()\n");
  392. tasklet_schedule(&ace->fsm_tasklet);
  393. ace->fsm_continue_flag = 0;
  394. }
  395. /* Set flag to exit FSM loop and wait for IRQ to reschedule tasklet */
  396. static inline void ace_fsm_yieldirq(struct ace_device *ace)
  397. {
  398. dev_dbg(ace->dev, "ace_fsm_yieldirq()\n");
  399. if (ace->irq == NO_IRQ)
  400. /* No IRQ assigned, so need to poll */
  401. tasklet_schedule(&ace->fsm_tasklet);
  402. ace->fsm_continue_flag = 0;
  403. }
  404. /* Get the next read/write request; ending requests that we don't handle */
  405. struct request *ace_get_next_request(struct request_queue * q)
  406. {
  407. struct request *req;
  408. while ((req = elv_next_request(q)) != NULL) {
  409. if (blk_fs_request(req))
  410. break;
  411. end_request(req, 0);
  412. }
  413. return req;
  414. }
  415. static void ace_fsm_dostate(struct ace_device *ace)
  416. {
  417. struct request *req;
  418. u32 status;
  419. u16 val;
  420. int count;
  421. int i;
  422. #if defined(DEBUG)
  423. dev_dbg(ace->dev, "fsm_state=%i, id_req_count=%i\n",
  424. ace->fsm_state, ace->id_req_count);
  425. #endif
  426. switch (ace->fsm_state) {
  427. case ACE_FSM_STATE_IDLE:
  428. /* See if there is anything to do */
  429. if (ace->id_req_count || ace_get_next_request(ace->queue)) {
  430. ace->fsm_iter_num++;
  431. ace->fsm_state = ACE_FSM_STATE_REQ_LOCK;
  432. mod_timer(&ace->stall_timer, jiffies + HZ);
  433. if (!timer_pending(&ace->stall_timer))
  434. add_timer(&ace->stall_timer);
  435. break;
  436. }
  437. del_timer(&ace->stall_timer);
  438. ace->fsm_continue_flag = 0;
  439. break;
  440. case ACE_FSM_STATE_REQ_LOCK:
  441. if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) {
  442. /* Already have the lock, jump to next state */
  443. ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
  444. break;
  445. }
  446. /* Request the lock */
  447. val = ace_in(ace, ACE_CTRL);
  448. ace_out(ace, ACE_CTRL, val | ACE_CTRL_LOCKREQ);
  449. ace->fsm_state = ACE_FSM_STATE_WAIT_LOCK;
  450. break;
  451. case ACE_FSM_STATE_WAIT_LOCK:
  452. if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) {
  453. /* got the lock; move to next state */
  454. ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
  455. break;
  456. }
  457. /* wait a bit for the lock */
  458. ace_fsm_yield(ace);
  459. break;
  460. case ACE_FSM_STATE_WAIT_CFREADY:
  461. status = ace_in32(ace, ACE_STATUS);
  462. if (!(status & ACE_STATUS_RDYFORCFCMD) ||
  463. (status & ACE_STATUS_CFBSY)) {
  464. /* CF card isn't ready; it needs to be polled */
  465. ace_fsm_yield(ace);
  466. break;
  467. }
  468. /* Device is ready for command; determine what to do next */
  469. if (ace->id_req_count)
  470. ace->fsm_state = ACE_FSM_STATE_IDENTIFY_PREPARE;
  471. else
  472. ace->fsm_state = ACE_FSM_STATE_REQ_PREPARE;
  473. break;
  474. case ACE_FSM_STATE_IDENTIFY_PREPARE:
  475. /* Send identify command */
  476. ace->fsm_task = ACE_TASK_IDENTIFY;
  477. ace->data_ptr = &ace->cf_id;
  478. ace->data_count = ACE_BUF_PER_SECTOR;
  479. ace_out(ace, ACE_SECCNTCMD, ACE_SECCNTCMD_IDENTIFY);
  480. /* As per datasheet, put config controller in reset */
  481. val = ace_in(ace, ACE_CTRL);
  482. ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
  483. /* irq handler takes over from this point; wait for the
  484. * transfer to complete */
  485. ace->fsm_state = ACE_FSM_STATE_IDENTIFY_TRANSFER;
  486. ace_fsm_yieldirq(ace);
  487. break;
  488. case ACE_FSM_STATE_IDENTIFY_TRANSFER:
  489. /* Check that the sysace is ready to receive data */
  490. status = ace_in32(ace, ACE_STATUS);
  491. if (status & ACE_STATUS_CFBSY) {
  492. dev_dbg(ace->dev, "CFBSY set; t=%i iter=%i dc=%i\n",
  493. ace->fsm_task, ace->fsm_iter_num,
  494. ace->data_count);
  495. ace_fsm_yield(ace);
  496. break;
  497. }
  498. if (!(status & ACE_STATUS_DATABUFRDY)) {
  499. ace_fsm_yield(ace);
  500. break;
  501. }
  502. /* Transfer the next buffer */
  503. ace->reg_ops->datain(ace);
  504. ace->data_count--;
  505. /* If there are still buffers to be transfers; jump out here */
  506. if (ace->data_count != 0) {
  507. ace_fsm_yieldirq(ace);
  508. break;
  509. }
  510. /* transfer finished; kick state machine */
  511. dev_dbg(ace->dev, "identify finished\n");
  512. ace->fsm_state = ACE_FSM_STATE_IDENTIFY_COMPLETE;
  513. break;
  514. case ACE_FSM_STATE_IDENTIFY_COMPLETE:
  515. ace_fix_driveid(&ace->cf_id);
  516. ace_dump_mem(&ace->cf_id, 512); /* Debug: Dump out disk ID */
  517. if (ace->data_result) {
  518. /* Error occured, disable the disk */
  519. ace->media_change = 1;
  520. set_capacity(ace->gd, 0);
  521. dev_err(ace->dev, "error fetching CF id (%i)\n",
  522. ace->data_result);
  523. } else {
  524. ace->media_change = 0;
  525. /* Record disk parameters */
  526. set_capacity(ace->gd, ace->cf_id.lba_capacity);
  527. dev_info(ace->dev, "capacity: %i sectors\n",
  528. ace->cf_id.lba_capacity);
  529. }
  530. /* We're done, drop to IDLE state and notify waiters */
  531. ace->fsm_state = ACE_FSM_STATE_IDLE;
  532. ace->id_result = ace->data_result;
  533. while (ace->id_req_count) {
  534. complete(&ace->id_completion);
  535. ace->id_req_count--;
  536. }
  537. break;
  538. case ACE_FSM_STATE_REQ_PREPARE:
  539. req = ace_get_next_request(ace->queue);
  540. if (!req) {
  541. ace->fsm_state = ACE_FSM_STATE_IDLE;
  542. break;
  543. }
  544. /* Okay, it's a data request, set it up for transfer */
  545. dev_dbg(ace->dev,
  546. "request: sec=%lx hcnt=%lx, ccnt=%x, dir=%i\n",
  547. req->sector, req->hard_nr_sectors,
  548. req->current_nr_sectors, rq_data_dir(req));
  549. ace->req = req;
  550. ace->data_ptr = req->buffer;
  551. ace->data_count = req->current_nr_sectors * ACE_BUF_PER_SECTOR;
  552. ace_out32(ace, ACE_MPULBA, req->sector & 0x0FFFFFFF);
  553. count = req->hard_nr_sectors;
  554. if (rq_data_dir(req)) {
  555. /* Kick off write request */
  556. dev_dbg(ace->dev, "write data\n");
  557. ace->fsm_task = ACE_TASK_WRITE;
  558. ace_out(ace, ACE_SECCNTCMD,
  559. count | ACE_SECCNTCMD_WRITE_DATA);
  560. } else {
  561. /* Kick off read request */
  562. dev_dbg(ace->dev, "read data\n");
  563. ace->fsm_task = ACE_TASK_READ;
  564. ace_out(ace, ACE_SECCNTCMD,
  565. count | ACE_SECCNTCMD_READ_DATA);
  566. }
  567. /* As per datasheet, put config controller in reset */
  568. val = ace_in(ace, ACE_CTRL);
  569. ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
  570. /* Move to the transfer state. The systemace will raise
  571. * an interrupt once there is something to do
  572. */
  573. ace->fsm_state = ACE_FSM_STATE_REQ_TRANSFER;
  574. if (ace->fsm_task == ACE_TASK_READ)
  575. ace_fsm_yieldirq(ace); /* wait for data ready */
  576. break;
  577. case ACE_FSM_STATE_REQ_TRANSFER:
  578. /* Check that the sysace is ready to receive data */
  579. status = ace_in32(ace, ACE_STATUS);
  580. if (status & ACE_STATUS_CFBSY) {
  581. dev_dbg(ace->dev,
  582. "CFBSY set; t=%i iter=%i c=%i dc=%i irq=%i\n",
  583. ace->fsm_task, ace->fsm_iter_num,
  584. ace->req->current_nr_sectors * 16,
  585. ace->data_count, ace->in_irq);
  586. ace_fsm_yield(ace); /* need to poll CFBSY bit */
  587. break;
  588. }
  589. if (!(status & ACE_STATUS_DATABUFRDY)) {
  590. dev_dbg(ace->dev,
  591. "DATABUF not set; t=%i iter=%i c=%i dc=%i irq=%i\n",
  592. ace->fsm_task, ace->fsm_iter_num,
  593. ace->req->current_nr_sectors * 16,
  594. ace->data_count, ace->in_irq);
  595. ace_fsm_yieldirq(ace);
  596. break;
  597. }
  598. /* Transfer the next buffer */
  599. i = 16;
  600. if (ace->fsm_task == ACE_TASK_WRITE)
  601. ace->reg_ops->dataout(ace);
  602. else
  603. ace->reg_ops->datain(ace);
  604. ace->data_count--;
  605. /* If there are still buffers to be transfers; jump out here */
  606. if (ace->data_count != 0) {
  607. ace_fsm_yieldirq(ace);
  608. break;
  609. }
  610. /* bio finished; is there another one? */
  611. i = ace->req->current_nr_sectors;
  612. if (end_that_request_first(ace->req, 1, i)) {
  613. /* dev_dbg(ace->dev, "next block; h=%li c=%i\n",
  614. * ace->req->hard_nr_sectors,
  615. * ace->req->current_nr_sectors);
  616. */
  617. ace->data_ptr = ace->req->buffer;
  618. ace->data_count = ace->req->current_nr_sectors * 16;
  619. ace_fsm_yieldirq(ace);
  620. break;
  621. }
  622. ace->fsm_state = ACE_FSM_STATE_REQ_COMPLETE;
  623. break;
  624. case ACE_FSM_STATE_REQ_COMPLETE:
  625. /* Complete the block request */
  626. blkdev_dequeue_request(ace->req);
  627. end_that_request_last(ace->req, 1);
  628. ace->req = NULL;
  629. /* Finished request; go to idle state */
  630. ace->fsm_state = ACE_FSM_STATE_IDLE;
  631. break;
  632. default:
  633. ace->fsm_state = ACE_FSM_STATE_IDLE;
  634. break;
  635. }
  636. }
  637. static void ace_fsm_tasklet(unsigned long data)
  638. {
  639. struct ace_device *ace = (void *)data;
  640. unsigned long flags;
  641. spin_lock_irqsave(&ace->lock, flags);
  642. /* Loop over state machine until told to stop */
  643. ace->fsm_continue_flag = 1;
  644. while (ace->fsm_continue_flag)
  645. ace_fsm_dostate(ace);
  646. spin_unlock_irqrestore(&ace->lock, flags);
  647. }
  648. static void ace_stall_timer(unsigned long data)
  649. {
  650. struct ace_device *ace = (void *)data;
  651. unsigned long flags;
  652. dev_warn(ace->dev,
  653. "kicking stalled fsm; state=%i task=%i iter=%i dc=%i\n",
  654. ace->fsm_state, ace->fsm_task, ace->fsm_iter_num,
  655. ace->data_count);
  656. spin_lock_irqsave(&ace->lock, flags);
  657. /* Rearm the stall timer *before* entering FSM (which may then
  658. * delete the timer) */
  659. mod_timer(&ace->stall_timer, jiffies + HZ);
  660. /* Loop over state machine until told to stop */
  661. ace->fsm_continue_flag = 1;
  662. while (ace->fsm_continue_flag)
  663. ace_fsm_dostate(ace);
  664. spin_unlock_irqrestore(&ace->lock, flags);
  665. }
  666. /* ---------------------------------------------------------------------
  667. * Interrupt handling routines
  668. */
  669. static int ace_interrupt_checkstate(struct ace_device *ace)
  670. {
  671. u32 sreg = ace_in32(ace, ACE_STATUS);
  672. u16 creg = ace_in(ace, ACE_CTRL);
  673. /* Check for error occurance */
  674. if ((sreg & (ACE_STATUS_CFGERROR | ACE_STATUS_CFCERROR)) &&
  675. (creg & ACE_CTRL_ERRORIRQ)) {
  676. dev_err(ace->dev, "transfer failure\n");
  677. ace_dump_regs(ace);
  678. return -EIO;
  679. }
  680. return 0;
  681. }
  682. static irqreturn_t ace_interrupt(int irq, void *dev_id)
  683. {
  684. u16 creg;
  685. struct ace_device *ace = dev_id;
  686. /* be safe and get the lock */
  687. spin_lock(&ace->lock);
  688. ace->in_irq = 1;
  689. /* clear the interrupt */
  690. creg = ace_in(ace, ACE_CTRL);
  691. ace_out(ace, ACE_CTRL, creg | ACE_CTRL_RESETIRQ);
  692. ace_out(ace, ACE_CTRL, creg);
  693. /* check for IO failures */
  694. if (ace_interrupt_checkstate(ace))
  695. ace->data_result = -EIO;
  696. if (ace->fsm_task == 0) {
  697. dev_err(ace->dev,
  698. "spurious irq; stat=%.8x ctrl=%.8x cmd=%.4x\n",
  699. ace_in32(ace, ACE_STATUS), ace_in32(ace, ACE_CTRL),
  700. ace_in(ace, ACE_SECCNTCMD));
  701. dev_err(ace->dev, "fsm_task=%i fsm_state=%i data_count=%i\n",
  702. ace->fsm_task, ace->fsm_state, ace->data_count);
  703. }
  704. /* Loop over state machine until told to stop */
  705. ace->fsm_continue_flag = 1;
  706. while (ace->fsm_continue_flag)
  707. ace_fsm_dostate(ace);
  708. /* done with interrupt; drop the lock */
  709. ace->in_irq = 0;
  710. spin_unlock(&ace->lock);
  711. return IRQ_HANDLED;
  712. }
  713. /* ---------------------------------------------------------------------
  714. * Block ops
  715. */
  716. static void ace_request(struct request_queue * q)
  717. {
  718. struct request *req;
  719. struct ace_device *ace;
  720. req = ace_get_next_request(q);
  721. if (req) {
  722. ace = req->rq_disk->private_data;
  723. tasklet_schedule(&ace->fsm_tasklet);
  724. }
  725. }
  726. static int ace_media_changed(struct gendisk *gd)
  727. {
  728. struct ace_device *ace = gd->private_data;
  729. dev_dbg(ace->dev, "ace_media_changed(): %i\n", ace->media_change);
  730. return ace->media_change;
  731. }
  732. static int ace_revalidate_disk(struct gendisk *gd)
  733. {
  734. struct ace_device *ace = gd->private_data;
  735. unsigned long flags;
  736. dev_dbg(ace->dev, "ace_revalidate_disk()\n");
  737. if (ace->media_change) {
  738. dev_dbg(ace->dev, "requesting cf id and scheduling tasklet\n");
  739. spin_lock_irqsave(&ace->lock, flags);
  740. ace->id_req_count++;
  741. spin_unlock_irqrestore(&ace->lock, flags);
  742. tasklet_schedule(&ace->fsm_tasklet);
  743. wait_for_completion(&ace->id_completion);
  744. }
  745. dev_dbg(ace->dev, "revalidate complete\n");
  746. return ace->id_result;
  747. }
  748. static int ace_open(struct inode *inode, struct file *filp)
  749. {
  750. struct ace_device *ace = inode->i_bdev->bd_disk->private_data;
  751. unsigned long flags;
  752. dev_dbg(ace->dev, "ace_open() users=%i\n", ace->users + 1);
  753. filp->private_data = ace;
  754. spin_lock_irqsave(&ace->lock, flags);
  755. ace->users++;
  756. spin_unlock_irqrestore(&ace->lock, flags);
  757. check_disk_change(inode->i_bdev);
  758. return 0;
  759. }
  760. static int ace_release(struct inode *inode, struct file *filp)
  761. {
  762. struct ace_device *ace = inode->i_bdev->bd_disk->private_data;
  763. unsigned long flags;
  764. u16 val;
  765. dev_dbg(ace->dev, "ace_release() users=%i\n", ace->users - 1);
  766. spin_lock_irqsave(&ace->lock, flags);
  767. ace->users--;
  768. if (ace->users == 0) {
  769. val = ace_in(ace, ACE_CTRL);
  770. ace_out(ace, ACE_CTRL, val & ~ACE_CTRL_LOCKREQ);
  771. }
  772. spin_unlock_irqrestore(&ace->lock, flags);
  773. return 0;
  774. }
  775. static int ace_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  776. {
  777. struct ace_device *ace = bdev->bd_disk->private_data;
  778. dev_dbg(ace->dev, "ace_getgeo()\n");
  779. geo->heads = ace->cf_id.heads;
  780. geo->sectors = ace->cf_id.sectors;
  781. geo->cylinders = ace->cf_id.cyls;
  782. return 0;
  783. }
  784. static struct block_device_operations ace_fops = {
  785. .owner = THIS_MODULE,
  786. .open = ace_open,
  787. .release = ace_release,
  788. .media_changed = ace_media_changed,
  789. .revalidate_disk = ace_revalidate_disk,
  790. .getgeo = ace_getgeo,
  791. };
  792. /* --------------------------------------------------------------------
  793. * SystemACE device setup/teardown code
  794. */
  795. static int __devinit ace_setup(struct ace_device *ace)
  796. {
  797. u16 version;
  798. u16 val;
  799. int rc;
  800. spin_lock_init(&ace->lock);
  801. init_completion(&ace->id_completion);
  802. /*
  803. * Map the device
  804. */
  805. ace->baseaddr = ioremap(ace->physaddr, 0x80);
  806. if (!ace->baseaddr)
  807. goto err_ioremap;
  808. if (ace->irq != NO_IRQ) {
  809. rc = request_irq(ace->irq, ace_interrupt, 0, "systemace", ace);
  810. if (rc) {
  811. /* Failure - fall back to polled mode */
  812. dev_err(ace->dev, "request_irq failed\n");
  813. ace->irq = NO_IRQ;
  814. }
  815. }
  816. /*
  817. * Initialize the state machine tasklet and stall timer
  818. */
  819. tasklet_init(&ace->fsm_tasklet, ace_fsm_tasklet, (unsigned long)ace);
  820. setup_timer(&ace->stall_timer, ace_stall_timer, (unsigned long)ace);
  821. /*
  822. * Initialize the request queue
  823. */
  824. ace->queue = blk_init_queue(ace_request, &ace->lock);
  825. if (ace->queue == NULL)
  826. goto err_blk_initq;
  827. blk_queue_hardsect_size(ace->queue, 512);
  828. /*
  829. * Allocate and initialize GD structure
  830. */
  831. ace->gd = alloc_disk(ACE_NUM_MINORS);
  832. if (!ace->gd)
  833. goto err_alloc_disk;
  834. ace->gd->major = ace_major;
  835. ace->gd->first_minor = ace->id * ACE_NUM_MINORS;
  836. ace->gd->fops = &ace_fops;
  837. ace->gd->queue = ace->queue;
  838. ace->gd->private_data = ace;
  839. snprintf(ace->gd->disk_name, 32, "xs%c", ace->id + 'a');
  840. /* set bus width */
  841. if (ace->bus_width == 1) {
  842. /* 0x0101 should work regardless of endianess */
  843. ace_out_le16(ace, ACE_BUSMODE, 0x0101);
  844. /* read it back to determine endianess */
  845. if (ace_in_le16(ace, ACE_BUSMODE) == 0x0001)
  846. ace->reg_ops = &ace_reg_le16_ops;
  847. else
  848. ace->reg_ops = &ace_reg_be16_ops;
  849. } else {
  850. ace_out_8(ace, ACE_BUSMODE, 0x00);
  851. ace->reg_ops = &ace_reg_8_ops;
  852. }
  853. /* Make sure version register is sane */
  854. version = ace_in(ace, ACE_VERSION);
  855. if ((version == 0) || (version == 0xFFFF))
  856. goto err_read;
  857. /* Put sysace in a sane state by clearing most control reg bits */
  858. ace_out(ace, ACE_CTRL, ACE_CTRL_FORCECFGMODE |
  859. ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ);
  860. /* Enable interrupts */
  861. val = ace_in(ace, ACE_CTRL);
  862. val |= ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ;
  863. ace_out(ace, ACE_CTRL, val);
  864. /* Print the identification */
  865. dev_info(ace->dev, "Xilinx SystemACE revision %i.%i.%i\n",
  866. (version >> 12) & 0xf, (version >> 8) & 0x0f, version & 0xff);
  867. dev_dbg(ace->dev, "physaddr 0x%lx, mapped to 0x%p, irq=%i\n",
  868. ace->physaddr, ace->baseaddr, ace->irq);
  869. ace->media_change = 1;
  870. ace_revalidate_disk(ace->gd);
  871. /* Make the sysace device 'live' */
  872. add_disk(ace->gd);
  873. return 0;
  874. err_read:
  875. put_disk(ace->gd);
  876. err_alloc_disk:
  877. blk_cleanup_queue(ace->queue);
  878. err_blk_initq:
  879. iounmap(ace->baseaddr);
  880. if (ace->irq != NO_IRQ)
  881. free_irq(ace->irq, ace);
  882. err_ioremap:
  883. printk(KERN_INFO "xsysace: error initializing device at 0x%lx\n",
  884. ace->physaddr);
  885. return -ENOMEM;
  886. }
  887. static void __devexit ace_teardown(struct ace_device *ace)
  888. {
  889. if (ace->gd) {
  890. del_gendisk(ace->gd);
  891. put_disk(ace->gd);
  892. }
  893. if (ace->queue)
  894. blk_cleanup_queue(ace->queue);
  895. tasklet_kill(&ace->fsm_tasklet);
  896. if (ace->irq != NO_IRQ)
  897. free_irq(ace->irq, ace);
  898. iounmap(ace->baseaddr);
  899. }
  900. /* ---------------------------------------------------------------------
  901. * Platform Bus Support
  902. */
  903. static int __devinit ace_probe(struct platform_device *dev)
  904. {
  905. struct ace_device *ace;
  906. int i;
  907. dev_dbg(&dev->dev, "ace_probe(%p)\n", dev);
  908. /*
  909. * Allocate the ace device structure
  910. */
  911. ace = kzalloc(sizeof(struct ace_device), GFP_KERNEL);
  912. if (!ace)
  913. goto err_alloc;
  914. ace->dev = &dev->dev;
  915. ace->id = dev->id;
  916. ace->irq = NO_IRQ;
  917. for (i = 0; i < dev->num_resources; i++) {
  918. if (dev->resource[i].flags & IORESOURCE_MEM)
  919. ace->physaddr = dev->resource[i].start;
  920. if (dev->resource[i].flags & IORESOURCE_IRQ)
  921. ace->irq = dev->resource[i].start;
  922. }
  923. /* FIXME: Should get bus_width from the platform_device struct */
  924. ace->bus_width = 1;
  925. platform_set_drvdata(dev, ace);
  926. /* Call the bus-independant setup code */
  927. if (ace_setup(ace) != 0)
  928. goto err_setup;
  929. return 0;
  930. err_setup:
  931. platform_set_drvdata(dev, NULL);
  932. kfree(ace);
  933. err_alloc:
  934. printk(KERN_ERR "xsysace: could not initialize device\n");
  935. return -ENOMEM;
  936. }
  937. /*
  938. * Platform bus remove() method
  939. */
  940. static int __devexit ace_remove(struct platform_device *dev)
  941. {
  942. struct ace_device *ace = platform_get_drvdata(dev);
  943. dev_dbg(&dev->dev, "ace_remove(%p)\n", dev);
  944. if (ace) {
  945. ace_teardown(ace);
  946. platform_set_drvdata(dev, NULL);
  947. kfree(ace);
  948. }
  949. return 0;
  950. }
  951. static struct platform_driver ace_platform_driver = {
  952. .probe = ace_probe,
  953. .remove = __devexit_p(ace_remove),
  954. .driver = {
  955. .owner = THIS_MODULE,
  956. .name = "xsysace",
  957. },
  958. };
  959. /* ---------------------------------------------------------------------
  960. * Module init/exit routines
  961. */
  962. static int __init ace_init(void)
  963. {
  964. int rc;
  965. ace_major = register_blkdev(ace_major, "xsysace");
  966. if (ace_major <= 0) {
  967. rc = -ENOMEM;
  968. goto err_blk;
  969. }
  970. if ((rc = platform_driver_register(&ace_platform_driver)) != 0)
  971. goto err_plat;
  972. pr_info("Xilinx SystemACE device driver, major=%i\n", ace_major);
  973. return 0;
  974. err_plat:
  975. unregister_blkdev(ace_major, "xsysace");
  976. err_blk:
  977. printk(KERN_ERR "xsysace: registration failed; err=%i\n", rc);
  978. return rc;
  979. }
  980. static void __exit ace_exit(void)
  981. {
  982. pr_debug("Unregistering Xilinx SystemACE driver\n");
  983. platform_driver_unregister(&ace_platform_driver);
  984. unregister_blkdev(ace_major, "xsysace");
  985. }
  986. module_init(ace_init);
  987. module_exit(ace_exit);