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