cafe.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * Driver for One Laptop Per Child ‘CAFÉ’ controller, aka Marvell 88ALP01
  3. *
  4. * Copyright © 2006 Red Hat, Inc.
  5. * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
  6. */
  7. #define DEBUG
  8. #include <linux/device.h>
  9. #undef DEBUG
  10. #include <linux/mtd/mtd.h>
  11. #include <linux/mtd/nand.h>
  12. #include <linux/pci.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/dma-mapping.h>
  16. #include <asm/io.h>
  17. #define CAFE_NAND_CTRL1 0x00
  18. #define CAFE_NAND_CTRL2 0x04
  19. #define CAFE_NAND_CTRL3 0x08
  20. #define CAFE_NAND_STATUS 0x0c
  21. #define CAFE_NAND_IRQ 0x10
  22. #define CAFE_NAND_IRQ_MASK 0x14
  23. #define CAFE_NAND_DATA_LEN 0x18
  24. #define CAFE_NAND_ADDR1 0x1c
  25. #define CAFE_NAND_ADDR2 0x20
  26. #define CAFE_NAND_TIMING1 0x24
  27. #define CAFE_NAND_TIMING2 0x28
  28. #define CAFE_NAND_TIMING3 0x2c
  29. #define CAFE_NAND_NONMEM 0x30
  30. #define CAFE_NAND_ECC_RESULT 0x3C
  31. #define CAFE_NAND_DMA_CTRL 0x40
  32. #define CAFE_NAND_DMA_ADDR0 0x44
  33. #define CAFE_NAND_DMA_ADDR1 0x48
  34. #define CAFE_NAND_ECC_SYN01 0x50
  35. #define CAFE_NAND_ECC_SYN23 0x54
  36. #define CAFE_NAND_ECC_SYN45 0x58
  37. #define CAFE_NAND_ECC_SYN67 0x5c
  38. #define CAFE_NAND_READ_DATA 0x1000
  39. #define CAFE_NAND_WRITE_DATA 0x2000
  40. #define CAFE_GLOBAL_CTRL 0x3004
  41. #define CAFE_GLOBAL_IRQ 0x3008
  42. #define CAFE_GLOBAL_IRQ_MASK 0x300c
  43. #define CAFE_NAND_RESET 0x3034
  44. int cafe_correct_ecc(unsigned char *buf,
  45. unsigned short *chk_syndrome_list);
  46. struct cafe_priv {
  47. struct nand_chip nand;
  48. struct pci_dev *pdev;
  49. void __iomem *mmio;
  50. uint32_t ctl1;
  51. uint32_t ctl2;
  52. int datalen;
  53. int nr_data;
  54. int data_pos;
  55. int page_addr;
  56. dma_addr_t dmaaddr;
  57. unsigned char *dmabuf;
  58. };
  59. static int usedma = 1;
  60. module_param(usedma, int, 0644);
  61. static int skipbbt = 0;
  62. module_param(skipbbt, int, 0644);
  63. static int debug = 0;
  64. module_param(debug, int, 0644);
  65. static int regdebug = 0;
  66. module_param(regdebug, int, 0644);
  67. static int checkecc = 1;
  68. module_param(checkecc, int, 0644);
  69. static int slowtiming = 0;
  70. module_param(slowtiming, int, 0644);
  71. /* Hrm. Why isn't this already conditional on something in the struct device? */
  72. #define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
  73. /* Make it easier to switch to PIO if we need to */
  74. #define cafe_readl(cafe, addr) readl((cafe)->mmio + CAFE_##addr)
  75. #define cafe_writel(cafe, datum, addr) writel(datum, (cafe)->mmio + CAFE_##addr)
  76. static int cafe_device_ready(struct mtd_info *mtd)
  77. {
  78. struct cafe_priv *cafe = mtd->priv;
  79. int result = !!(cafe_readl(cafe, NAND_STATUS) | 0x40000000);
  80. uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
  81. cafe_writel(cafe, irqs, NAND_IRQ);
  82. cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
  83. result?"":" not", irqs, cafe_readl(cafe, NAND_IRQ),
  84. cafe_readl(cafe, GLOBAL_IRQ), cafe_readl(cafe, GLOBAL_IRQ_MASK));
  85. return result;
  86. }
  87. static void cafe_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  88. {
  89. struct cafe_priv *cafe = mtd->priv;
  90. if (usedma)
  91. memcpy(cafe->dmabuf + cafe->datalen, buf, len);
  92. else
  93. memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
  94. cafe->datalen += len;
  95. cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
  96. len, cafe->datalen);
  97. }
  98. static void cafe_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  99. {
  100. struct cafe_priv *cafe = mtd->priv;
  101. if (usedma)
  102. memcpy(buf, cafe->dmabuf + cafe->datalen, len);
  103. else
  104. memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
  105. cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes from position 0x%x in read buffer.\n",
  106. len, cafe->datalen);
  107. cafe->datalen += len;
  108. }
  109. static uint8_t cafe_read_byte(struct mtd_info *mtd)
  110. {
  111. struct cafe_priv *cafe = mtd->priv;
  112. uint8_t d;
  113. cafe_read_buf(mtd, &d, 1);
  114. cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
  115. return d;
  116. }
  117. static void cafe_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  118. int column, int page_addr)
  119. {
  120. struct cafe_priv *cafe = mtd->priv;
  121. int adrbytes = 0;
  122. uint32_t ctl1;
  123. uint32_t doneint = 0x80000000;
  124. cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
  125. command, column, page_addr);
  126. if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
  127. /* Second half of a command we already calculated */
  128. cafe_writel(cafe, cafe->ctl2 | 0x100 | command, NAND_CTRL2);
  129. ctl1 = cafe->ctl1;
  130. cafe->ctl2 &= ~(1<<30);
  131. cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
  132. cafe->ctl1, cafe->nr_data);
  133. goto do_command;
  134. }
  135. /* Reset ECC engine */
  136. cafe_writel(cafe, 0, NAND_CTRL2);
  137. /* Emulate NAND_CMD_READOOB on large-page chips */
  138. if (mtd->writesize > 512 &&
  139. command == NAND_CMD_READOOB) {
  140. column += mtd->writesize;
  141. command = NAND_CMD_READ0;
  142. }
  143. /* FIXME: Do we need to send read command before sending data
  144. for small-page chips, to position the buffer correctly? */
  145. if (column != -1) {
  146. cafe_writel(cafe, column, NAND_ADDR1);
  147. adrbytes = 2;
  148. if (page_addr != -1)
  149. goto write_adr2;
  150. } else if (page_addr != -1) {
  151. cafe_writel(cafe, page_addr & 0xffff, NAND_ADDR1);
  152. page_addr >>= 16;
  153. write_adr2:
  154. cafe_writel(cafe, page_addr, NAND_ADDR2);
  155. adrbytes += 2;
  156. if (mtd->size > mtd->writesize << 16)
  157. adrbytes++;
  158. }
  159. cafe->data_pos = cafe->datalen = 0;
  160. /* Set command valid bit */
  161. ctl1 = 0x80000000 | command;
  162. /* Set RD or WR bits as appropriate */
  163. if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
  164. ctl1 |= (1<<26); /* rd */
  165. /* Always 5 bytes, for now */
  166. cafe->datalen = 4;
  167. /* And one address cycle -- even for STATUS, since the controller doesn't work without */
  168. adrbytes = 1;
  169. } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
  170. command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
  171. ctl1 |= 1<<26; /* rd */
  172. /* For now, assume just read to end of page */
  173. cafe->datalen = mtd->writesize + mtd->oobsize - column;
  174. } else if (command == NAND_CMD_SEQIN)
  175. ctl1 |= 1<<25; /* wr */
  176. /* Set number of address bytes */
  177. if (adrbytes)
  178. ctl1 |= ((adrbytes-1)|8) << 27;
  179. if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
  180. /* Ignore the first command of a pair; the hardware
  181. deals with them both at once, later */
  182. cafe->ctl1 = ctl1;
  183. cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
  184. cafe->ctl1, cafe->datalen);
  185. return;
  186. }
  187. /* RNDOUT and READ0 commands need a following byte */
  188. if (command == NAND_CMD_RNDOUT)
  189. cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, NAND_CTRL2);
  190. else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
  191. cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_READSTART, NAND_CTRL2);
  192. do_command:
  193. cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
  194. cafe->datalen, ctl1, cafe_readl(cafe, NAND_CTRL2));
  195. /* NB: The datasheet lies -- we really should be subtracting 1 here */
  196. cafe_writel(cafe, cafe->datalen, NAND_DATA_LEN);
  197. cafe_writel(cafe, 0x90000000, NAND_IRQ);
  198. if (usedma && (ctl1 & (3<<25))) {
  199. uint32_t dmactl = 0xc0000000 + cafe->datalen;
  200. /* If WR or RD bits set, set up DMA */
  201. if (ctl1 & (1<<26)) {
  202. /* It's a read */
  203. dmactl |= (1<<29);
  204. /* ... so it's done when the DMA is done, not just
  205. the command. */
  206. doneint = 0x10000000;
  207. }
  208. cafe_writel(cafe, dmactl, NAND_DMA_CTRL);
  209. }
  210. cafe->datalen = 0;
  211. if (unlikely(regdebug)) {
  212. int i;
  213. printk("About to write command %08x to register 0\n", ctl1);
  214. for (i=4; i< 0x5c; i+=4)
  215. printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
  216. }
  217. cafe_writel(cafe, ctl1, NAND_CTRL1);
  218. /* Apply this short delay always to ensure that we do wait tWB in
  219. * any case on any machine. */
  220. ndelay(100);
  221. if (1) {
  222. int c = 500000;
  223. uint32_t irqs;
  224. while (c--) {
  225. irqs = cafe_readl(cafe, NAND_IRQ);
  226. if (irqs & doneint)
  227. break;
  228. udelay(1);
  229. if (!(c % 100000))
  230. cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
  231. cpu_relax();
  232. }
  233. cafe_writel(cafe, doneint, NAND_IRQ);
  234. cafe_dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n",
  235. command, 500000-c, irqs, cafe_readl(cafe, NAND_IRQ));
  236. }
  237. WARN_ON(cafe->ctl2 & (1<<30));
  238. switch (command) {
  239. case NAND_CMD_CACHEDPROG:
  240. case NAND_CMD_PAGEPROG:
  241. case NAND_CMD_ERASE1:
  242. case NAND_CMD_ERASE2:
  243. case NAND_CMD_SEQIN:
  244. case NAND_CMD_RNDIN:
  245. case NAND_CMD_STATUS:
  246. case NAND_CMD_DEPLETE1:
  247. case NAND_CMD_RNDOUT:
  248. case NAND_CMD_STATUS_ERROR:
  249. case NAND_CMD_STATUS_ERROR0:
  250. case NAND_CMD_STATUS_ERROR1:
  251. case NAND_CMD_STATUS_ERROR2:
  252. case NAND_CMD_STATUS_ERROR3:
  253. cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
  254. return;
  255. }
  256. nand_wait_ready(mtd);
  257. cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
  258. }
  259. static void cafe_select_chip(struct mtd_info *mtd, int chipnr)
  260. {
  261. //struct cafe_priv *cafe = mtd->priv;
  262. // cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
  263. }
  264. static int cafe_nand_interrupt(int irq, void *id)
  265. {
  266. struct mtd_info *mtd = id;
  267. struct cafe_priv *cafe = mtd->priv;
  268. uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
  269. cafe_writel(cafe, irqs & ~0x90000000, NAND_IRQ);
  270. if (!irqs)
  271. return IRQ_NONE;
  272. cafe_dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, cafe_readl(cafe, NAND_IRQ));
  273. return IRQ_HANDLED;
  274. }
  275. static void cafe_nand_bug(struct mtd_info *mtd)
  276. {
  277. BUG();
  278. }
  279. static int cafe_nand_write_oob(struct mtd_info *mtd,
  280. struct nand_chip *chip, int page)
  281. {
  282. int status = 0;
  283. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  284. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  285. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  286. status = chip->waitfunc(mtd, chip);
  287. return status & NAND_STATUS_FAIL ? -EIO : 0;
  288. }
  289. /* Don't use -- use nand_read_oob_std for now */
  290. static int cafe_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  291. int page, int sndcmd)
  292. {
  293. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  294. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  295. return 1;
  296. }
  297. /**
  298. * cafe_nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
  299. * @mtd: mtd info structure
  300. * @chip: nand chip info structure
  301. * @buf: buffer to store read data
  302. *
  303. * The hw generator calculates the error syndrome automatically. Therefor
  304. * we need a special oob layout and handling.
  305. */
  306. static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  307. uint8_t *buf)
  308. {
  309. struct cafe_priv *cafe = mtd->priv;
  310. cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
  311. cafe_readl(cafe, NAND_ECC_RESULT),
  312. cafe_readl(cafe, NAND_ECC_SYN01));
  313. chip->read_buf(mtd, buf, mtd->writesize);
  314. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  315. if (checkecc && cafe_readl(cafe, NAND_ECC_RESULT) & (1<<18)) {
  316. unsigned short syn[8];
  317. int i;
  318. for (i=0; i<8; i+=2) {
  319. uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2));
  320. syn[i] = tmp & 0xfff;
  321. syn[i+1] = (tmp >> 16) & 0xfff;
  322. }
  323. if ((i = cafe_correct_ecc(buf, syn)) < 0) {
  324. dev_dbg(&cafe->pdev->dev, "Failed to correct ECC at %08x\n",
  325. cafe_readl(cafe, NAND_ADDR2) * 2048);
  326. for (i=0; i< 0x5c; i+=4)
  327. printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
  328. mtd->ecc_stats.failed++;
  329. } else {
  330. dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", i);
  331. mtd->ecc_stats.corrected += i;
  332. }
  333. }
  334. return 0;
  335. }
  336. static struct nand_ecclayout cafe_oobinfo_2048 = {
  337. .eccbytes = 14,
  338. .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
  339. .oobfree = {{14, 50}}
  340. };
  341. /* Ick. The BBT code really ought to be able to work this bit out
  342. for itself from the above, at least for the 2KiB case */
  343. static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
  344. static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
  345. static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
  346. static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
  347. static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
  348. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  349. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  350. .offs = 14,
  351. .len = 4,
  352. .veroffs = 18,
  353. .maxblocks = 4,
  354. .pattern = cafe_bbt_pattern_2048
  355. };
  356. static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
  357. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  358. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  359. .offs = 14,
  360. .len = 4,
  361. .veroffs = 18,
  362. .maxblocks = 4,
  363. .pattern = cafe_mirror_pattern_2048
  364. };
  365. static struct nand_ecclayout cafe_oobinfo_512 = {
  366. .eccbytes = 14,
  367. .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
  368. .oobfree = {{14, 2}}
  369. };
  370. static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
  371. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  372. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  373. .offs = 14,
  374. .len = 1,
  375. .veroffs = 15,
  376. .maxblocks = 4,
  377. .pattern = cafe_bbt_pattern_512
  378. };
  379. static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
  380. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  381. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  382. .offs = 14,
  383. .len = 1,
  384. .veroffs = 15,
  385. .maxblocks = 4,
  386. .pattern = cafe_mirror_pattern_512
  387. };
  388. static void cafe_nand_write_page_lowlevel(struct mtd_info *mtd,
  389. struct nand_chip *chip, const uint8_t *buf)
  390. {
  391. struct cafe_priv *cafe = mtd->priv;
  392. chip->write_buf(mtd, buf, mtd->writesize);
  393. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  394. /* Set up ECC autogeneration */
  395. cafe->ctl2 |= (1<<30);
  396. }
  397. static int cafe_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  398. const uint8_t *buf, int page, int cached, int raw)
  399. {
  400. int status;
  401. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  402. if (unlikely(raw))
  403. chip->ecc.write_page_raw(mtd, chip, buf);
  404. else
  405. chip->ecc.write_page(mtd, chip, buf);
  406. /*
  407. * Cached progamming disabled for now, Not sure if its worth the
  408. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
  409. */
  410. cached = 0;
  411. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  412. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  413. status = chip->waitfunc(mtd, chip);
  414. /*
  415. * See if operation failed and additional status checks are
  416. * available
  417. */
  418. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  419. status = chip->errstat(mtd, chip, FL_WRITING, status,
  420. page);
  421. if (status & NAND_STATUS_FAIL)
  422. return -EIO;
  423. } else {
  424. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  425. status = chip->waitfunc(mtd, chip);
  426. }
  427. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  428. /* Send command to read back the data */
  429. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  430. if (chip->verify_buf(mtd, buf, mtd->writesize))
  431. return -EIO;
  432. #endif
  433. return 0;
  434. }
  435. static int cafe_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  436. {
  437. return 0;
  438. }
  439. static int __devinit cafe_nand_probe(struct pci_dev *pdev,
  440. const struct pci_device_id *ent)
  441. {
  442. struct mtd_info *mtd;
  443. struct cafe_priv *cafe;
  444. uint32_t ctrl;
  445. int err = 0;
  446. err = pci_enable_device(pdev);
  447. if (err)
  448. return err;
  449. pci_set_master(pdev);
  450. mtd = kzalloc(sizeof(*mtd) + sizeof(struct cafe_priv), GFP_KERNEL);
  451. if (!mtd) {
  452. dev_warn(&pdev->dev, "failed to alloc mtd_info\n");
  453. return -ENOMEM;
  454. }
  455. cafe = (void *)(&mtd[1]);
  456. mtd->priv = cafe;
  457. mtd->owner = THIS_MODULE;
  458. cafe->pdev = pdev;
  459. cafe->mmio = pci_iomap(pdev, 0, 0);
  460. if (!cafe->mmio) {
  461. dev_warn(&pdev->dev, "failed to iomap\n");
  462. err = -ENOMEM;
  463. goto out_free_mtd;
  464. }
  465. cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
  466. &cafe->dmaaddr, GFP_KERNEL);
  467. if (!cafe->dmabuf) {
  468. err = -ENOMEM;
  469. goto out_ior;
  470. }
  471. cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
  472. cafe->nand.cmdfunc = cafe_nand_cmdfunc;
  473. cafe->nand.dev_ready = cafe_device_ready;
  474. cafe->nand.read_byte = cafe_read_byte;
  475. cafe->nand.read_buf = cafe_read_buf;
  476. cafe->nand.write_buf = cafe_write_buf;
  477. cafe->nand.select_chip = cafe_select_chip;
  478. cafe->nand.chip_delay = 0;
  479. /* Enable the following for a flash based bad block table */
  480. cafe->nand.options = NAND_USE_FLASH_BBT | NAND_NO_AUTOINCR | NAND_OWN_BUFFERS;
  481. if (skipbbt) {
  482. cafe->nand.options |= NAND_SKIP_BBTSCAN;
  483. cafe->nand.block_bad = cafe_nand_block_bad;
  484. }
  485. /* Start off by resetting the NAND controller completely */
  486. cafe_writel(cafe, 1, NAND_RESET);
  487. cafe_writel(cafe, 0, NAND_RESET);
  488. cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
  489. /* Timings from Marvell's test code (not verified or calculated by us) */
  490. if (!slowtiming) {
  491. cafe_writel(cafe, 0x01010a0a, NAND_TIMING1);
  492. cafe_writel(cafe, 0x24121212, NAND_TIMING2);
  493. cafe_writel(cafe, 0x11000000, NAND_TIMING3);
  494. } else {
  495. cafe_writel(cafe, 0xffffffff, NAND_TIMING1);
  496. cafe_writel(cafe, 0xffffffff, NAND_TIMING2);
  497. cafe_writel(cafe, 0xffffffff, NAND_TIMING3);
  498. }
  499. cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
  500. err = request_irq(pdev->irq, &cafe_nand_interrupt, SA_SHIRQ, "CAFE NAND", mtd);
  501. if (err) {
  502. dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
  503. goto out_free_dma;
  504. }
  505. #if 1
  506. /* Disable master reset, enable NAND clock */
  507. ctrl = cafe_readl(cafe, GLOBAL_CTRL);
  508. ctrl &= 0xffffeff0;
  509. ctrl |= 0x00007000;
  510. cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
  511. cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
  512. cafe_writel(cafe, 0, NAND_DMA_CTRL);
  513. cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
  514. cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
  515. /* Set up DMA address */
  516. cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
  517. if (sizeof(cafe->dmaaddr) > 4)
  518. /* Shift in two parts to shut the compiler up */
  519. cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
  520. else
  521. cafe_writel(cafe, 0, NAND_DMA_ADDR1);
  522. cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
  523. cafe_readl(cafe, NAND_DMA_ADDR0), cafe->dmabuf);
  524. /* Enable NAND IRQ in global IRQ mask register */
  525. cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
  526. cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
  527. cafe_readl(cafe, GLOBAL_CTRL), cafe_readl(cafe, GLOBAL_IRQ_MASK));
  528. #endif
  529. #if 1
  530. mtd->writesize=2048;
  531. mtd->oobsize = 0x40;
  532. memset(cafe->dmabuf, 0x5a, 2112);
  533. cafe->nand.cmdfunc(mtd, NAND_CMD_READID, 0, -1);
  534. cafe->nand.read_byte(mtd);
  535. cafe->nand.read_byte(mtd);
  536. cafe->nand.read_byte(mtd);
  537. cafe->nand.read_byte(mtd);
  538. cafe->nand.read_byte(mtd);
  539. #endif
  540. #if 0
  541. cafe->nand.cmdfunc(mtd, NAND_CMD_READ0, 0, 0);
  542. // nand_wait_ready(mtd);
  543. cafe->nand.read_byte(mtd);
  544. cafe->nand.read_byte(mtd);
  545. cafe->nand.read_byte(mtd);
  546. cafe->nand.read_byte(mtd);
  547. #endif
  548. #if 0
  549. writel(0x84600070, cafe->mmio);
  550. udelay(10);
  551. cafe_dev_dbg(&cafe->pdev->dev, "Status %x\n", cafe_readl(cafe, NAND_NONMEM));
  552. #endif
  553. /* Scan to find existance of the device */
  554. if (nand_scan_ident(mtd, 1)) {
  555. err = -ENXIO;
  556. goto out_irq;
  557. }
  558. cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
  559. if (mtd->writesize == 2048)
  560. cafe->ctl2 |= 1<<29; /* 2KiB page size */
  561. /* Set up ECC according to the type of chip we found */
  562. if (mtd->writesize == 2048) {
  563. cafe->nand.ecc.layout = &cafe_oobinfo_2048;
  564. cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
  565. cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
  566. } else if (mtd->writesize == 512) {
  567. cafe->nand.ecc.layout = &cafe_oobinfo_512;
  568. cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
  569. cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
  570. } else {
  571. printk(KERN_WARNING "Unexpected NAND flash writesize %d. Aborting\n",
  572. mtd->writesize);
  573. goto out_irq;
  574. }
  575. cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
  576. cafe->nand.ecc.size = mtd->writesize;
  577. cafe->nand.ecc.bytes = 14;
  578. cafe->nand.ecc.hwctl = (void *)cafe_nand_bug;
  579. cafe->nand.ecc.calculate = (void *)cafe_nand_bug;
  580. cafe->nand.ecc.correct = (void *)cafe_nand_bug;
  581. cafe->nand.write_page = cafe_nand_write_page;
  582. cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
  583. cafe->nand.ecc.write_oob = cafe_nand_write_oob;
  584. cafe->nand.ecc.read_page = cafe_nand_read_page;
  585. cafe->nand.ecc.read_oob = cafe_nand_read_oob;
  586. err = nand_scan_tail(mtd);
  587. if (err)
  588. goto out_irq;
  589. pci_set_drvdata(pdev, mtd);
  590. add_mtd_device(mtd);
  591. goto out;
  592. out_irq:
  593. /* Disable NAND IRQ in global IRQ mask register */
  594. cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
  595. free_irq(pdev->irq, mtd);
  596. out_free_dma:
  597. dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
  598. out_ior:
  599. pci_iounmap(pdev, cafe->mmio);
  600. out_free_mtd:
  601. kfree(mtd);
  602. out:
  603. return err;
  604. }
  605. static void __devexit cafe_nand_remove(struct pci_dev *pdev)
  606. {
  607. struct mtd_info *mtd = pci_get_drvdata(pdev);
  608. struct cafe_priv *cafe = mtd->priv;
  609. del_mtd_device(mtd);
  610. /* Disable NAND IRQ in global IRQ mask register */
  611. cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
  612. free_irq(pdev->irq, mtd);
  613. nand_release(mtd);
  614. pci_iounmap(pdev, cafe->mmio);
  615. dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
  616. kfree(mtd);
  617. }
  618. static struct pci_device_id cafe_nand_tbl[] = {
  619. { 0x11ab, 0x4100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MEMORY_FLASH << 8, 0xFFFF0 }
  620. };
  621. MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
  622. static struct pci_driver cafe_nand_pci_driver = {
  623. .name = "CAFÉ NAND",
  624. .id_table = cafe_nand_tbl,
  625. .probe = cafe_nand_probe,
  626. .remove = __devexit_p(cafe_nand_remove),
  627. #ifdef CONFIG_PMx
  628. .suspend = cafe_nand_suspend,
  629. .resume = cafe_nand_resume,
  630. #endif
  631. };
  632. static int cafe_nand_init(void)
  633. {
  634. return pci_register_driver(&cafe_nand_pci_driver);
  635. }
  636. static void cafe_nand_exit(void)
  637. {
  638. pci_unregister_driver(&cafe_nand_pci_driver);
  639. }
  640. module_init(cafe_nand_init);
  641. module_exit(cafe_nand_exit);
  642. MODULE_LICENSE("GPL");
  643. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  644. MODULE_DESCRIPTION("NAND flash driver for OLPC CAFE chip");
  645. /* Correct ECC for 2048 bytes of 0xff:
  646. 41 a0 71 65 54 27 f3 93 ec a9 be ed 0b a1 */
  647. /* dwmw2's B-test board, in case of completely screwing it:
  648. Bad eraseblock 2394 at 0x12b40000
  649. Bad eraseblock 2627 at 0x14860000
  650. Bad eraseblock 3349 at 0x1a2a0000
  651. */