cafe_nand.c 25 KB

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