xsysace.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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. #define ACE_BUS_WIDTH_8 0
  149. #define ACE_BUS_WIDTH_16 1
  150. struct ace_reg_ops;
  151. struct ace_device {
  152. /* driver state data */
  153. int id;
  154. int media_change;
  155. int users;
  156. struct list_head list;
  157. /* finite state machine data */
  158. struct tasklet_struct fsm_tasklet;
  159. uint fsm_task; /* Current activity (ACE_TASK_*) */
  160. uint fsm_state; /* Current state (ACE_FSM_STATE_*) */
  161. uint fsm_continue_flag; /* cleared to exit FSM mainloop */
  162. uint fsm_iter_num;
  163. struct timer_list stall_timer;
  164. /* Transfer state/result, use for both id and block request */
  165. struct request *req; /* request being processed */
  166. void *data_ptr; /* pointer to I/O buffer */
  167. int data_count; /* number of buffers remaining */
  168. int data_result; /* Result of transfer; 0 := success */
  169. int id_req_count; /* count of id requests */
  170. int id_result;
  171. struct completion id_completion; /* used when id req finishes */
  172. int in_irq;
  173. /* Details of hardware device */
  174. unsigned long physaddr;
  175. void *baseaddr;
  176. int irq;
  177. int bus_width; /* 0 := 8 bit; 1 := 16 bit */
  178. struct ace_reg_ops *reg_ops;
  179. int lock_count;
  180. /* Block device data structures */
  181. spinlock_t lock;
  182. struct device *dev;
  183. struct request_queue *queue;
  184. struct gendisk *gd;
  185. /* Inserted CF card parameters */
  186. struct hd_driveid cf_id;
  187. };
  188. static int ace_major;
  189. /* ---------------------------------------------------------------------
  190. * Low level register access
  191. */
  192. struct ace_reg_ops {
  193. u16(*in) (struct ace_device * ace, int reg);
  194. void (*out) (struct ace_device * ace, int reg, u16 val);
  195. void (*datain) (struct ace_device * ace);
  196. void (*dataout) (struct ace_device * ace);
  197. };
  198. /* 8 Bit bus width */
  199. static u16 ace_in_8(struct ace_device *ace, int reg)
  200. {
  201. void *r = ace->baseaddr + reg;
  202. return in_8(r) | (in_8(r + 1) << 8);
  203. }
  204. static void ace_out_8(struct ace_device *ace, int reg, u16 val)
  205. {
  206. void *r = ace->baseaddr + reg;
  207. out_8(r, val);
  208. out_8(r + 1, val >> 8);
  209. }
  210. static void ace_datain_8(struct ace_device *ace)
  211. {
  212. void *r = ace->baseaddr + 0x40;
  213. u8 *dst = ace->data_ptr;
  214. int i = ACE_FIFO_SIZE;
  215. while (i--)
  216. *dst++ = in_8(r++);
  217. ace->data_ptr = dst;
  218. }
  219. static void ace_dataout_8(struct ace_device *ace)
  220. {
  221. void *r = ace->baseaddr + 0x40;
  222. u8 *src = ace->data_ptr;
  223. int i = ACE_FIFO_SIZE;
  224. while (i--)
  225. out_8(r++, *src++);
  226. ace->data_ptr = src;
  227. }
  228. static struct ace_reg_ops ace_reg_8_ops = {
  229. .in = ace_in_8,
  230. .out = ace_out_8,
  231. .datain = ace_datain_8,
  232. .dataout = ace_dataout_8,
  233. };
  234. /* 16 bit big endian bus attachment */
  235. static u16 ace_in_be16(struct ace_device *ace, int reg)
  236. {
  237. return in_be16(ace->baseaddr + reg);
  238. }
  239. static void ace_out_be16(struct ace_device *ace, int reg, u16 val)
  240. {
  241. out_be16(ace->baseaddr + reg, val);
  242. }
  243. static void ace_datain_be16(struct ace_device *ace)
  244. {
  245. int i = ACE_FIFO_SIZE / 2;
  246. u16 *dst = ace->data_ptr;
  247. while (i--)
  248. *dst++ = in_le16(ace->baseaddr + 0x40);
  249. ace->data_ptr = dst;
  250. }
  251. static void ace_dataout_be16(struct ace_device *ace)
  252. {
  253. int i = ACE_FIFO_SIZE / 2;
  254. u16 *src = ace->data_ptr;
  255. while (i--)
  256. out_le16(ace->baseaddr + 0x40, *src++);
  257. ace->data_ptr = src;
  258. }
  259. /* 16 bit little endian bus attachment */
  260. static u16 ace_in_le16(struct ace_device *ace, int reg)
  261. {
  262. return in_le16(ace->baseaddr + reg);
  263. }
  264. static void ace_out_le16(struct ace_device *ace, int reg, u16 val)
  265. {
  266. out_le16(ace->baseaddr + reg, val);
  267. }
  268. static void ace_datain_le16(struct ace_device *ace)
  269. {
  270. int i = ACE_FIFO_SIZE / 2;
  271. u16 *dst = ace->data_ptr;
  272. while (i--)
  273. *dst++ = in_be16(ace->baseaddr + 0x40);
  274. ace->data_ptr = dst;
  275. }
  276. static void ace_dataout_le16(struct ace_device *ace)
  277. {
  278. int i = ACE_FIFO_SIZE / 2;
  279. u16 *src = ace->data_ptr;
  280. while (i--)
  281. out_be16(ace->baseaddr + 0x40, *src++);
  282. ace->data_ptr = src;
  283. }
  284. static struct ace_reg_ops ace_reg_be16_ops = {
  285. .in = ace_in_be16,
  286. .out = ace_out_be16,
  287. .datain = ace_datain_be16,
  288. .dataout = ace_dataout_be16,
  289. };
  290. static struct ace_reg_ops ace_reg_le16_ops = {
  291. .in = ace_in_le16,
  292. .out = ace_out_le16,
  293. .datain = ace_datain_le16,
  294. .dataout = ace_dataout_le16,
  295. };
  296. static inline u16 ace_in(struct ace_device *ace, int reg)
  297. {
  298. return ace->reg_ops->in(ace, reg);
  299. }
  300. static inline u32 ace_in32(struct ace_device *ace, int reg)
  301. {
  302. return ace_in(ace, reg) | (ace_in(ace, reg + 2) << 16);
  303. }
  304. static inline void ace_out(struct ace_device *ace, int reg, u16 val)
  305. {
  306. ace->reg_ops->out(ace, reg, val);
  307. }
  308. static inline void ace_out32(struct ace_device *ace, int reg, u32 val)
  309. {
  310. ace_out(ace, reg, val);
  311. ace_out(ace, reg + 2, val >> 16);
  312. }
  313. /* ---------------------------------------------------------------------
  314. * Debug support functions
  315. */
  316. #if defined(DEBUG)
  317. static void ace_dump_mem(void *base, int len)
  318. {
  319. const char *ptr = base;
  320. int i, j;
  321. for (i = 0; i < len; i += 16) {
  322. printk(KERN_INFO "%.8x:", i);
  323. for (j = 0; j < 16; j++) {
  324. if (!(j % 4))
  325. printk(" ");
  326. printk("%.2x", ptr[i + j]);
  327. }
  328. printk(" ");
  329. for (j = 0; j < 16; j++)
  330. printk("%c", isprint(ptr[i + j]) ? ptr[i + j] : '.');
  331. printk("\n");
  332. }
  333. }
  334. #else
  335. static inline void ace_dump_mem(void *base, int len)
  336. {
  337. }
  338. #endif
  339. static void ace_dump_regs(struct ace_device *ace)
  340. {
  341. dev_info(ace->dev, " ctrl: %.8x seccnt/cmd: %.4x ver:%.4x\n"
  342. " status:%.8x mpu_lba:%.8x busmode:%4x\n"
  343. " error: %.8x cfg_lba:%.8x fatstat:%.4x\n",
  344. ace_in32(ace, ACE_CTRL),
  345. ace_in(ace, ACE_SECCNTCMD),
  346. ace_in(ace, ACE_VERSION),
  347. ace_in32(ace, ACE_STATUS),
  348. ace_in32(ace, ACE_MPULBA),
  349. ace_in(ace, ACE_BUSMODE),
  350. ace_in32(ace, ACE_ERROR),
  351. ace_in32(ace, ACE_CFGLBA), ace_in(ace, ACE_FATSTAT));
  352. }
  353. void ace_fix_driveid(struct hd_driveid *id)
  354. {
  355. #if defined(__BIG_ENDIAN)
  356. u16 *buf = (void *)id;
  357. int i;
  358. /* All half words have wrong byte order; swap the bytes */
  359. for (i = 0; i < sizeof(struct hd_driveid); i += 2, buf++)
  360. *buf = le16_to_cpu(*buf);
  361. /* Some of the data values are 32bit; swap the half words */
  362. id->lba_capacity = ((id->lba_capacity >> 16) & 0x0000FFFF) |
  363. ((id->lba_capacity << 16) & 0xFFFF0000);
  364. id->spg = ((id->spg >> 16) & 0x0000FFFF) |
  365. ((id->spg << 16) & 0xFFFF0000);
  366. #endif
  367. }
  368. /* ---------------------------------------------------------------------
  369. * Finite State Machine (FSM) implementation
  370. */
  371. /* FSM tasks; used to direct state transitions */
  372. #define ACE_TASK_IDLE 0
  373. #define ACE_TASK_IDENTIFY 1
  374. #define ACE_TASK_READ 2
  375. #define ACE_TASK_WRITE 3
  376. #define ACE_FSM_NUM_TASKS 4
  377. /* FSM state definitions */
  378. #define ACE_FSM_STATE_IDLE 0
  379. #define ACE_FSM_STATE_REQ_LOCK 1
  380. #define ACE_FSM_STATE_WAIT_LOCK 2
  381. #define ACE_FSM_STATE_WAIT_CFREADY 3
  382. #define ACE_FSM_STATE_IDENTIFY_PREPARE 4
  383. #define ACE_FSM_STATE_IDENTIFY_TRANSFER 5
  384. #define ACE_FSM_STATE_IDENTIFY_COMPLETE 6
  385. #define ACE_FSM_STATE_REQ_PREPARE 7
  386. #define ACE_FSM_STATE_REQ_TRANSFER 8
  387. #define ACE_FSM_STATE_REQ_COMPLETE 9
  388. #define ACE_FSM_STATE_ERROR 10
  389. #define ACE_FSM_NUM_STATES 11
  390. /* Set flag to exit FSM loop and reschedule tasklet */
  391. static inline void ace_fsm_yield(struct ace_device *ace)
  392. {
  393. dev_dbg(ace->dev, "ace_fsm_yield()\n");
  394. tasklet_schedule(&ace->fsm_tasklet);
  395. ace->fsm_continue_flag = 0;
  396. }
  397. /* Set flag to exit FSM loop and wait for IRQ to reschedule tasklet */
  398. static inline void ace_fsm_yieldirq(struct ace_device *ace)
  399. {
  400. dev_dbg(ace->dev, "ace_fsm_yieldirq()\n");
  401. if (ace->irq == NO_IRQ)
  402. /* No IRQ assigned, so need to poll */
  403. tasklet_schedule(&ace->fsm_tasklet);
  404. ace->fsm_continue_flag = 0;
  405. }
  406. /* Get the next read/write request; ending requests that we don't handle */
  407. struct request *ace_get_next_request(struct request_queue * q)
  408. {
  409. struct request *req;
  410. while ((req = elv_next_request(q)) != NULL) {
  411. if (blk_fs_request(req))
  412. break;
  413. end_request(req, 0);
  414. }
  415. return req;
  416. }
  417. static void ace_fsm_dostate(struct ace_device *ace)
  418. {
  419. struct request *req;
  420. u32 status;
  421. u16 val;
  422. int count;
  423. int i;
  424. #if defined(DEBUG)
  425. dev_dbg(ace->dev, "fsm_state=%i, id_req_count=%i\n",
  426. ace->fsm_state, ace->id_req_count);
  427. #endif
  428. switch (ace->fsm_state) {
  429. case ACE_FSM_STATE_IDLE:
  430. /* See if there is anything to do */
  431. if (ace->id_req_count || ace_get_next_request(ace->queue)) {
  432. ace->fsm_iter_num++;
  433. ace->fsm_state = ACE_FSM_STATE_REQ_LOCK;
  434. mod_timer(&ace->stall_timer, jiffies + HZ);
  435. if (!timer_pending(&ace->stall_timer))
  436. add_timer(&ace->stall_timer);
  437. break;
  438. }
  439. del_timer(&ace->stall_timer);
  440. ace->fsm_continue_flag = 0;
  441. break;
  442. case ACE_FSM_STATE_REQ_LOCK:
  443. if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) {
  444. /* Already have the lock, jump to next state */
  445. ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
  446. break;
  447. }
  448. /* Request the lock */
  449. val = ace_in(ace, ACE_CTRL);
  450. ace_out(ace, ACE_CTRL, val | ACE_CTRL_LOCKREQ);
  451. ace->fsm_state = ACE_FSM_STATE_WAIT_LOCK;
  452. break;
  453. case ACE_FSM_STATE_WAIT_LOCK:
  454. if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) {
  455. /* got the lock; move to next state */
  456. ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
  457. break;
  458. }
  459. /* wait a bit for the lock */
  460. ace_fsm_yield(ace);
  461. break;
  462. case ACE_FSM_STATE_WAIT_CFREADY:
  463. status = ace_in32(ace, ACE_STATUS);
  464. if (!(status & ACE_STATUS_RDYFORCFCMD) ||
  465. (status & ACE_STATUS_CFBSY)) {
  466. /* CF card isn't ready; it needs to be polled */
  467. ace_fsm_yield(ace);
  468. break;
  469. }
  470. /* Device is ready for command; determine what to do next */
  471. if (ace->id_req_count)
  472. ace->fsm_state = ACE_FSM_STATE_IDENTIFY_PREPARE;
  473. else
  474. ace->fsm_state = ACE_FSM_STATE_REQ_PREPARE;
  475. break;
  476. case ACE_FSM_STATE_IDENTIFY_PREPARE:
  477. /* Send identify command */
  478. ace->fsm_task = ACE_TASK_IDENTIFY;
  479. ace->data_ptr = &ace->cf_id;
  480. ace->data_count = ACE_BUF_PER_SECTOR;
  481. ace_out(ace, ACE_SECCNTCMD, ACE_SECCNTCMD_IDENTIFY);
  482. /* As per datasheet, put config controller in reset */
  483. val = ace_in(ace, ACE_CTRL);
  484. ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
  485. /* irq handler takes over from this point; wait for the
  486. * transfer to complete */
  487. ace->fsm_state = ACE_FSM_STATE_IDENTIFY_TRANSFER;
  488. ace_fsm_yieldirq(ace);
  489. break;
  490. case ACE_FSM_STATE_IDENTIFY_TRANSFER:
  491. /* Check that the sysace is ready to receive data */
  492. status = ace_in32(ace, ACE_STATUS);
  493. if (status & ACE_STATUS_CFBSY) {
  494. dev_dbg(ace->dev, "CFBSY set; t=%i iter=%i dc=%i\n",
  495. ace->fsm_task, ace->fsm_iter_num,
  496. ace->data_count);
  497. ace_fsm_yield(ace);
  498. break;
  499. }
  500. if (!(status & ACE_STATUS_DATABUFRDY)) {
  501. ace_fsm_yield(ace);
  502. break;
  503. }
  504. /* Transfer the next buffer */
  505. ace->reg_ops->datain(ace);
  506. ace->data_count--;
  507. /* If there are still buffers to be transfers; jump out here */
  508. if (ace->data_count != 0) {
  509. ace_fsm_yieldirq(ace);
  510. break;
  511. }
  512. /* transfer finished; kick state machine */
  513. dev_dbg(ace->dev, "identify finished\n");
  514. ace->fsm_state = ACE_FSM_STATE_IDENTIFY_COMPLETE;
  515. break;
  516. case ACE_FSM_STATE_IDENTIFY_COMPLETE:
  517. ace_fix_driveid(&ace->cf_id);
  518. ace_dump_mem(&ace->cf_id, 512); /* Debug: Dump out disk ID */
  519. if (ace->data_result) {
  520. /* Error occured, disable the disk */
  521. ace->media_change = 1;
  522. set_capacity(ace->gd, 0);
  523. dev_err(ace->dev, "error fetching CF id (%i)\n",
  524. ace->data_result);
  525. } else {
  526. ace->media_change = 0;
  527. /* Record disk parameters */
  528. set_capacity(ace->gd, ace->cf_id.lba_capacity);
  529. dev_info(ace->dev, "capacity: %i sectors\n",
  530. ace->cf_id.lba_capacity);
  531. }
  532. /* We're done, drop to IDLE state and notify waiters */
  533. ace->fsm_state = ACE_FSM_STATE_IDLE;
  534. ace->id_result = ace->data_result;
  535. while (ace->id_req_count) {
  536. complete(&ace->id_completion);
  537. ace->id_req_count--;
  538. }
  539. break;
  540. case ACE_FSM_STATE_REQ_PREPARE:
  541. req = ace_get_next_request(ace->queue);
  542. if (!req) {
  543. ace->fsm_state = ACE_FSM_STATE_IDLE;
  544. break;
  545. }
  546. /* Okay, it's a data request, set it up for transfer */
  547. dev_dbg(ace->dev,
  548. "request: sec=%lx hcnt=%lx, ccnt=%x, dir=%i\n",
  549. req->sector, req->hard_nr_sectors,
  550. req->current_nr_sectors, rq_data_dir(req));
  551. ace->req = req;
  552. ace->data_ptr = req->buffer;
  553. ace->data_count = req->current_nr_sectors * ACE_BUF_PER_SECTOR;
  554. ace_out32(ace, ACE_MPULBA, req->sector & 0x0FFFFFFF);
  555. count = req->hard_nr_sectors;
  556. if (rq_data_dir(req)) {
  557. /* Kick off write request */
  558. dev_dbg(ace->dev, "write data\n");
  559. ace->fsm_task = ACE_TASK_WRITE;
  560. ace_out(ace, ACE_SECCNTCMD,
  561. count | ACE_SECCNTCMD_WRITE_DATA);
  562. } else {
  563. /* Kick off read request */
  564. dev_dbg(ace->dev, "read data\n");
  565. ace->fsm_task = ACE_TASK_READ;
  566. ace_out(ace, ACE_SECCNTCMD,
  567. count | ACE_SECCNTCMD_READ_DATA);
  568. }
  569. /* As per datasheet, put config controller in reset */
  570. val = ace_in(ace, ACE_CTRL);
  571. ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
  572. /* Move to the transfer state. The systemace will raise
  573. * an interrupt once there is something to do
  574. */
  575. ace->fsm_state = ACE_FSM_STATE_REQ_TRANSFER;
  576. if (ace->fsm_task == ACE_TASK_READ)
  577. ace_fsm_yieldirq(ace); /* wait for data ready */
  578. break;
  579. case ACE_FSM_STATE_REQ_TRANSFER:
  580. /* Check that the sysace is ready to receive data */
  581. status = ace_in32(ace, ACE_STATUS);
  582. if (status & ACE_STATUS_CFBSY) {
  583. dev_dbg(ace->dev,
  584. "CFBSY set; t=%i iter=%i c=%i dc=%i irq=%i\n",
  585. ace->fsm_task, ace->fsm_iter_num,
  586. ace->req->current_nr_sectors * 16,
  587. ace->data_count, ace->in_irq);
  588. ace_fsm_yield(ace); /* need to poll CFBSY bit */
  589. break;
  590. }
  591. if (!(status & ACE_STATUS_DATABUFRDY)) {
  592. dev_dbg(ace->dev,
  593. "DATABUF not set; t=%i iter=%i c=%i dc=%i irq=%i\n",
  594. ace->fsm_task, ace->fsm_iter_num,
  595. ace->req->current_nr_sectors * 16,
  596. ace->data_count, ace->in_irq);
  597. ace_fsm_yieldirq(ace);
  598. break;
  599. }
  600. /* Transfer the next buffer */
  601. i = 16;
  602. if (ace->fsm_task == ACE_TASK_WRITE)
  603. ace->reg_ops->dataout(ace);
  604. else
  605. ace->reg_ops->datain(ace);
  606. ace->data_count--;
  607. /* If there are still buffers to be transfers; jump out here */
  608. if (ace->data_count != 0) {
  609. ace_fsm_yieldirq(ace);
  610. break;
  611. }
  612. /* bio finished; is there another one? */
  613. i = ace->req->current_nr_sectors;
  614. if (end_that_request_first(ace->req, 1, i)) {
  615. /* dev_dbg(ace->dev, "next block; h=%li c=%i\n",
  616. * ace->req->hard_nr_sectors,
  617. * ace->req->current_nr_sectors);
  618. */
  619. ace->data_ptr = ace->req->buffer;
  620. ace->data_count = ace->req->current_nr_sectors * 16;
  621. ace_fsm_yieldirq(ace);
  622. break;
  623. }
  624. ace->fsm_state = ACE_FSM_STATE_REQ_COMPLETE;
  625. break;
  626. case ACE_FSM_STATE_REQ_COMPLETE:
  627. /* Complete the block request */
  628. blkdev_dequeue_request(ace->req);
  629. end_that_request_last(ace->req, 1);
  630. ace->req = NULL;
  631. /* Finished request; go to idle state */
  632. ace->fsm_state = ACE_FSM_STATE_IDLE;
  633. break;
  634. default:
  635. ace->fsm_state = ACE_FSM_STATE_IDLE;
  636. break;
  637. }
  638. }
  639. static void ace_fsm_tasklet(unsigned long data)
  640. {
  641. struct ace_device *ace = (void *)data;
  642. unsigned long flags;
  643. spin_lock_irqsave(&ace->lock, flags);
  644. /* Loop over state machine until told to stop */
  645. ace->fsm_continue_flag = 1;
  646. while (ace->fsm_continue_flag)
  647. ace_fsm_dostate(ace);
  648. spin_unlock_irqrestore(&ace->lock, flags);
  649. }
  650. static void ace_stall_timer(unsigned long data)
  651. {
  652. struct ace_device *ace = (void *)data;
  653. unsigned long flags;
  654. dev_warn(ace->dev,
  655. "kicking stalled fsm; state=%i task=%i iter=%i dc=%i\n",
  656. ace->fsm_state, ace->fsm_task, ace->fsm_iter_num,
  657. ace->data_count);
  658. spin_lock_irqsave(&ace->lock, flags);
  659. /* Rearm the stall timer *before* entering FSM (which may then
  660. * delete the timer) */
  661. mod_timer(&ace->stall_timer, jiffies + HZ);
  662. /* Loop over state machine until told to stop */
  663. ace->fsm_continue_flag = 1;
  664. while (ace->fsm_continue_flag)
  665. ace_fsm_dostate(ace);
  666. spin_unlock_irqrestore(&ace->lock, flags);
  667. }
  668. /* ---------------------------------------------------------------------
  669. * Interrupt handling routines
  670. */
  671. static int ace_interrupt_checkstate(struct ace_device *ace)
  672. {
  673. u32 sreg = ace_in32(ace, ACE_STATUS);
  674. u16 creg = ace_in(ace, ACE_CTRL);
  675. /* Check for error occurance */
  676. if ((sreg & (ACE_STATUS_CFGERROR | ACE_STATUS_CFCERROR)) &&
  677. (creg & ACE_CTRL_ERRORIRQ)) {
  678. dev_err(ace->dev, "transfer failure\n");
  679. ace_dump_regs(ace);
  680. return -EIO;
  681. }
  682. return 0;
  683. }
  684. static irqreturn_t ace_interrupt(int irq, void *dev_id)
  685. {
  686. u16 creg;
  687. struct ace_device *ace = dev_id;
  688. /* be safe and get the lock */
  689. spin_lock(&ace->lock);
  690. ace->in_irq = 1;
  691. /* clear the interrupt */
  692. creg = ace_in(ace, ACE_CTRL);
  693. ace_out(ace, ACE_CTRL, creg | ACE_CTRL_RESETIRQ);
  694. ace_out(ace, ACE_CTRL, creg);
  695. /* check for IO failures */
  696. if (ace_interrupt_checkstate(ace))
  697. ace->data_result = -EIO;
  698. if (ace->fsm_task == 0) {
  699. dev_err(ace->dev,
  700. "spurious irq; stat=%.8x ctrl=%.8x cmd=%.4x\n",
  701. ace_in32(ace, ACE_STATUS), ace_in32(ace, ACE_CTRL),
  702. ace_in(ace, ACE_SECCNTCMD));
  703. dev_err(ace->dev, "fsm_task=%i fsm_state=%i data_count=%i\n",
  704. ace->fsm_task, ace->fsm_state, ace->data_count);
  705. }
  706. /* Loop over state machine until told to stop */
  707. ace->fsm_continue_flag = 1;
  708. while (ace->fsm_continue_flag)
  709. ace_fsm_dostate(ace);
  710. /* done with interrupt; drop the lock */
  711. ace->in_irq = 0;
  712. spin_unlock(&ace->lock);
  713. return IRQ_HANDLED;
  714. }
  715. /* ---------------------------------------------------------------------
  716. * Block ops
  717. */
  718. static void ace_request(struct request_queue * q)
  719. {
  720. struct request *req;
  721. struct ace_device *ace;
  722. req = ace_get_next_request(q);
  723. if (req) {
  724. ace = req->rq_disk->private_data;
  725. tasklet_schedule(&ace->fsm_tasklet);
  726. }
  727. }
  728. static int ace_media_changed(struct gendisk *gd)
  729. {
  730. struct ace_device *ace = gd->private_data;
  731. dev_dbg(ace->dev, "ace_media_changed(): %i\n", ace->media_change);
  732. return ace->media_change;
  733. }
  734. static int ace_revalidate_disk(struct gendisk *gd)
  735. {
  736. struct ace_device *ace = gd->private_data;
  737. unsigned long flags;
  738. dev_dbg(ace->dev, "ace_revalidate_disk()\n");
  739. if (ace->media_change) {
  740. dev_dbg(ace->dev, "requesting cf id and scheduling tasklet\n");
  741. spin_lock_irqsave(&ace->lock, flags);
  742. ace->id_req_count++;
  743. spin_unlock_irqrestore(&ace->lock, flags);
  744. tasklet_schedule(&ace->fsm_tasklet);
  745. wait_for_completion(&ace->id_completion);
  746. }
  747. dev_dbg(ace->dev, "revalidate complete\n");
  748. return ace->id_result;
  749. }
  750. static int ace_open(struct inode *inode, struct file *filp)
  751. {
  752. struct ace_device *ace = inode->i_bdev->bd_disk->private_data;
  753. unsigned long flags;
  754. dev_dbg(ace->dev, "ace_open() users=%i\n", ace->users + 1);
  755. filp->private_data = ace;
  756. spin_lock_irqsave(&ace->lock, flags);
  757. ace->users++;
  758. spin_unlock_irqrestore(&ace->lock, flags);
  759. check_disk_change(inode->i_bdev);
  760. return 0;
  761. }
  762. static int ace_release(struct inode *inode, struct file *filp)
  763. {
  764. struct ace_device *ace = inode->i_bdev->bd_disk->private_data;
  765. unsigned long flags;
  766. u16 val;
  767. dev_dbg(ace->dev, "ace_release() users=%i\n", ace->users - 1);
  768. spin_lock_irqsave(&ace->lock, flags);
  769. ace->users--;
  770. if (ace->users == 0) {
  771. val = ace_in(ace, ACE_CTRL);
  772. ace_out(ace, ACE_CTRL, val & ~ACE_CTRL_LOCKREQ);
  773. }
  774. spin_unlock_irqrestore(&ace->lock, flags);
  775. return 0;
  776. }
  777. static int ace_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  778. {
  779. struct ace_device *ace = bdev->bd_disk->private_data;
  780. dev_dbg(ace->dev, "ace_getgeo()\n");
  781. geo->heads = ace->cf_id.heads;
  782. geo->sectors = ace->cf_id.sectors;
  783. geo->cylinders = ace->cf_id.cyls;
  784. return 0;
  785. }
  786. static struct block_device_operations ace_fops = {
  787. .owner = THIS_MODULE,
  788. .open = ace_open,
  789. .release = ace_release,
  790. .media_changed = ace_media_changed,
  791. .revalidate_disk = ace_revalidate_disk,
  792. .getgeo = ace_getgeo,
  793. };
  794. /* --------------------------------------------------------------------
  795. * SystemACE device setup/teardown code
  796. */
  797. static int __devinit ace_setup(struct ace_device *ace)
  798. {
  799. u16 version;
  800. u16 val;
  801. int rc;
  802. dev_dbg(ace->dev, "ace_setup(ace=0x%p)\n", ace);
  803. dev_dbg(ace->dev, "physaddr=0x%lx irq=%i\n", ace->physaddr, ace->irq);
  804. spin_lock_init(&ace->lock);
  805. init_completion(&ace->id_completion);
  806. /*
  807. * Map the device
  808. */
  809. ace->baseaddr = ioremap(ace->physaddr, 0x80);
  810. if (!ace->baseaddr)
  811. goto err_ioremap;
  812. if (ace->irq != NO_IRQ) {
  813. rc = request_irq(ace->irq, ace_interrupt, 0, "systemace", ace);
  814. if (rc) {
  815. /* Failure - fall back to polled mode */
  816. dev_err(ace->dev, "request_irq failed\n");
  817. ace->irq = NO_IRQ;
  818. }
  819. }
  820. /*
  821. * Initialize the state machine tasklet and stall timer
  822. */
  823. tasklet_init(&ace->fsm_tasklet, ace_fsm_tasklet, (unsigned long)ace);
  824. setup_timer(&ace->stall_timer, ace_stall_timer, (unsigned long)ace);
  825. /*
  826. * Initialize the request queue
  827. */
  828. ace->queue = blk_init_queue(ace_request, &ace->lock);
  829. if (ace->queue == NULL)
  830. goto err_blk_initq;
  831. blk_queue_hardsect_size(ace->queue, 512);
  832. /*
  833. * Allocate and initialize GD structure
  834. */
  835. ace->gd = alloc_disk(ACE_NUM_MINORS);
  836. if (!ace->gd)
  837. goto err_alloc_disk;
  838. ace->gd->major = ace_major;
  839. ace->gd->first_minor = ace->id * ACE_NUM_MINORS;
  840. ace->gd->fops = &ace_fops;
  841. ace->gd->queue = ace->queue;
  842. ace->gd->private_data = ace;
  843. snprintf(ace->gd->disk_name, 32, "xs%c", ace->id + 'a');
  844. /* set bus width */
  845. if (ace->bus_width == ACE_BUS_WIDTH_16) {
  846. /* 0x0101 should work regardless of endianess */
  847. ace_out_le16(ace, ACE_BUSMODE, 0x0101);
  848. /* read it back to determine endianess */
  849. if (ace_in_le16(ace, ACE_BUSMODE) == 0x0001)
  850. ace->reg_ops = &ace_reg_le16_ops;
  851. else
  852. ace->reg_ops = &ace_reg_be16_ops;
  853. } else {
  854. ace_out_8(ace, ACE_BUSMODE, 0x00);
  855. ace->reg_ops = &ace_reg_8_ops;
  856. }
  857. /* Make sure version register is sane */
  858. version = ace_in(ace, ACE_VERSION);
  859. if ((version == 0) || (version == 0xFFFF))
  860. goto err_read;
  861. /* Put sysace in a sane state by clearing most control reg bits */
  862. ace_out(ace, ACE_CTRL, ACE_CTRL_FORCECFGMODE |
  863. ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ);
  864. /* Enable interrupts */
  865. val = ace_in(ace, ACE_CTRL);
  866. val |= ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ;
  867. ace_out(ace, ACE_CTRL, val);
  868. /* Print the identification */
  869. dev_info(ace->dev, "Xilinx SystemACE revision %i.%i.%i\n",
  870. (version >> 12) & 0xf, (version >> 8) & 0x0f, version & 0xff);
  871. dev_dbg(ace->dev, "physaddr 0x%lx, mapped to 0x%p, irq=%i\n",
  872. ace->physaddr, ace->baseaddr, ace->irq);
  873. ace->media_change = 1;
  874. ace_revalidate_disk(ace->gd);
  875. /* Make the sysace device 'live' */
  876. add_disk(ace->gd);
  877. return 0;
  878. err_read:
  879. put_disk(ace->gd);
  880. err_alloc_disk:
  881. blk_cleanup_queue(ace->queue);
  882. err_blk_initq:
  883. iounmap(ace->baseaddr);
  884. if (ace->irq != NO_IRQ)
  885. free_irq(ace->irq, ace);
  886. err_ioremap:
  887. dev_info(ace->dev, "xsysace: error initializing device at 0x%lx\n",
  888. ace->physaddr);
  889. return -ENOMEM;
  890. }
  891. static void __devexit ace_teardown(struct ace_device *ace)
  892. {
  893. if (ace->gd) {
  894. del_gendisk(ace->gd);
  895. put_disk(ace->gd);
  896. }
  897. if (ace->queue)
  898. blk_cleanup_queue(ace->queue);
  899. tasklet_kill(&ace->fsm_tasklet);
  900. if (ace->irq != NO_IRQ)
  901. free_irq(ace->irq, ace);
  902. iounmap(ace->baseaddr);
  903. }
  904. static int __devinit
  905. ace_alloc(struct device *dev, int id, unsigned long physaddr,
  906. int irq, int bus_width)
  907. {
  908. struct ace_device *ace;
  909. int rc;
  910. dev_dbg(dev, "ace_alloc(%p)\n", dev);
  911. if (!physaddr) {
  912. rc = -ENODEV;
  913. goto err_noreg;
  914. }
  915. /* Allocate and initialize the ace device structure */
  916. ace = kzalloc(sizeof(struct ace_device), GFP_KERNEL);
  917. if (!ace) {
  918. rc = -ENOMEM;
  919. goto err_alloc;
  920. }
  921. ace->dev = dev;
  922. ace->id = id;
  923. ace->physaddr = physaddr;
  924. ace->irq = irq;
  925. ace->bus_width = bus_width;
  926. /* Call the setup code */
  927. if ((rc = ace_setup(ace)) != 0)
  928. goto err_setup;
  929. dev_set_drvdata(dev, ace);
  930. return 0;
  931. err_setup:
  932. dev_set_drvdata(dev, NULL);
  933. kfree(ace);
  934. err_alloc:
  935. err_noreg:
  936. dev_err(dev, "could not initialize device, err=%i\n", rc);
  937. return rc;
  938. }
  939. static void __devexit ace_free(struct device *dev)
  940. {
  941. struct ace_device *ace = dev_get_drvdata(dev);
  942. dev_dbg(dev, "ace_free(%p)\n", dev);
  943. if (ace) {
  944. ace_teardown(ace);
  945. dev_set_drvdata(dev, NULL);
  946. kfree(ace);
  947. }
  948. }
  949. /* ---------------------------------------------------------------------
  950. * Platform Bus Support
  951. */
  952. static int __devinit ace_probe(struct platform_device *dev)
  953. {
  954. unsigned long physaddr = 0;
  955. int bus_width = ACE_BUS_WIDTH_16; /* FIXME: should not be hard coded */
  956. int id = dev->id;
  957. int irq = NO_IRQ;
  958. int i;
  959. dev_dbg(&dev->dev, "ace_probe(%p)\n", dev);
  960. for (i = 0; i < dev->num_resources; i++) {
  961. if (dev->resource[i].flags & IORESOURCE_MEM)
  962. physaddr = dev->resource[i].start;
  963. if (dev->resource[i].flags & IORESOURCE_IRQ)
  964. irq = dev->resource[i].start;
  965. }
  966. /* Call the bus-independant setup code */
  967. return ace_alloc(&dev->dev, id, physaddr, irq, bus_width);
  968. }
  969. /*
  970. * Platform bus remove() method
  971. */
  972. static int __devexit ace_remove(struct platform_device *dev)
  973. {
  974. ace_free(&dev->dev);
  975. return 0;
  976. }
  977. static struct platform_driver ace_platform_driver = {
  978. .probe = ace_probe,
  979. .remove = __devexit_p(ace_remove),
  980. .driver = {
  981. .owner = THIS_MODULE,
  982. .name = "xsysace",
  983. },
  984. };
  985. /* ---------------------------------------------------------------------
  986. * Module init/exit routines
  987. */
  988. static int __init ace_init(void)
  989. {
  990. int rc;
  991. ace_major = register_blkdev(ace_major, "xsysace");
  992. if (ace_major <= 0) {
  993. rc = -ENOMEM;
  994. goto err_blk;
  995. }
  996. pr_debug("xsysace: registering platform binding\n");
  997. if ((rc = platform_driver_register(&ace_platform_driver)) != 0)
  998. goto err_plat;
  999. pr_info("Xilinx SystemACE device driver, major=%i\n", ace_major);
  1000. return 0;
  1001. err_plat:
  1002. unregister_blkdev(ace_major, "xsysace");
  1003. err_blk:
  1004. printk(KERN_ERR "xsysace: registration failed; err=%i\n", rc);
  1005. return rc;
  1006. }
  1007. static void __exit ace_exit(void)
  1008. {
  1009. pr_debug("Unregistering Xilinx SystemACE driver\n");
  1010. platform_driver_unregister(&ace_platform_driver);
  1011. unregister_blkdev(ace_major, "xsysace");
  1012. }
  1013. module_init(ace_init);
  1014. module_exit(ace_exit);