cafe_nand.c 25 KB

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