cafe_nand.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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)
  308. {
  309. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  310. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  311. return 0;
  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. * @oob_required: caller expects OOB data read to chip->oob_poi
  319. *
  320. * The hw generator calculates the error syndrome automatically. Therefor
  321. * we need a special oob layout and handling.
  322. */
  323. static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  324. uint8_t *buf, int oob_required, int page)
  325. {
  326. struct cafe_priv *cafe = mtd->priv;
  327. unsigned int max_bitflips = 0;
  328. cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
  329. cafe_readl(cafe, NAND_ECC_RESULT),
  330. cafe_readl(cafe, NAND_ECC_SYN01));
  331. chip->read_buf(mtd, buf, mtd->writesize);
  332. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  333. if (checkecc && cafe_readl(cafe, NAND_ECC_RESULT) & (1<<18)) {
  334. unsigned short syn[8], pat[4];
  335. int pos[4];
  336. u8 *oob = chip->oob_poi;
  337. int i, n;
  338. for (i=0; i<8; i+=2) {
  339. uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2));
  340. syn[i] = cafe->rs->index_of[tmp & 0xfff];
  341. syn[i+1] = cafe->rs->index_of[(tmp >> 16) & 0xfff];
  342. }
  343. n = decode_rs16(cafe->rs, NULL, NULL, 1367, syn, 0, pos, 0,
  344. pat);
  345. for (i = 0; i < n; i++) {
  346. int p = pos[i];
  347. /* The 12-bit symbols are mapped to bytes here */
  348. if (p > 1374) {
  349. /* out of range */
  350. n = -1374;
  351. } else if (p == 0) {
  352. /* high four bits do not correspond to data */
  353. if (pat[i] > 0xff)
  354. n = -2048;
  355. else
  356. buf[0] ^= pat[i];
  357. } else if (p == 1365) {
  358. buf[2047] ^= pat[i] >> 4;
  359. oob[0] ^= pat[i] << 4;
  360. } else if (p > 1365) {
  361. if ((p & 1) == 1) {
  362. oob[3*p/2 - 2048] ^= pat[i] >> 4;
  363. oob[3*p/2 - 2047] ^= pat[i] << 4;
  364. } else {
  365. oob[3*p/2 - 2049] ^= pat[i] >> 8;
  366. oob[3*p/2 - 2048] ^= pat[i];
  367. }
  368. } else if ((p & 1) == 1) {
  369. buf[3*p/2] ^= pat[i] >> 4;
  370. buf[3*p/2 + 1] ^= pat[i] << 4;
  371. } else {
  372. buf[3*p/2 - 1] ^= pat[i] >> 8;
  373. buf[3*p/2] ^= pat[i];
  374. }
  375. }
  376. if (n < 0) {
  377. dev_dbg(&cafe->pdev->dev, "Failed to correct ECC at %08x\n",
  378. cafe_readl(cafe, NAND_ADDR2) * 2048);
  379. for (i = 0; i < 0x5c; i += 4)
  380. printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
  381. mtd->ecc_stats.failed++;
  382. } else {
  383. dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", n);
  384. mtd->ecc_stats.corrected += n;
  385. max_bitflips = max_t(unsigned int, max_bitflips, n);
  386. }
  387. }
  388. return max_bitflips;
  389. }
  390. static struct nand_ecclayout cafe_oobinfo_2048 = {
  391. .eccbytes = 14,
  392. .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
  393. .oobfree = {{14, 50}}
  394. };
  395. /* Ick. The BBT code really ought to be able to work this bit out
  396. for itself from the above, at least for the 2KiB case */
  397. static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
  398. static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
  399. static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
  400. static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
  401. static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
  402. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  403. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  404. .offs = 14,
  405. .len = 4,
  406. .veroffs = 18,
  407. .maxblocks = 4,
  408. .pattern = cafe_bbt_pattern_2048
  409. };
  410. static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
  411. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  412. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  413. .offs = 14,
  414. .len = 4,
  415. .veroffs = 18,
  416. .maxblocks = 4,
  417. .pattern = cafe_mirror_pattern_2048
  418. };
  419. static struct nand_ecclayout cafe_oobinfo_512 = {
  420. .eccbytes = 14,
  421. .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
  422. .oobfree = {{14, 2}}
  423. };
  424. static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
  425. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  426. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  427. .offs = 14,
  428. .len = 1,
  429. .veroffs = 15,
  430. .maxblocks = 4,
  431. .pattern = cafe_bbt_pattern_512
  432. };
  433. static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
  434. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  435. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  436. .offs = 14,
  437. .len = 1,
  438. .veroffs = 15,
  439. .maxblocks = 4,
  440. .pattern = cafe_mirror_pattern_512
  441. };
  442. static void cafe_nand_write_page_lowlevel(struct mtd_info *mtd,
  443. struct nand_chip *chip,
  444. const uint8_t *buf, int oob_required)
  445. {
  446. struct cafe_priv *cafe = mtd->priv;
  447. chip->write_buf(mtd, buf, mtd->writesize);
  448. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  449. /* Set up ECC autogeneration */
  450. cafe->ctl2 |= (1<<30);
  451. }
  452. static int cafe_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  453. const uint8_t *buf, int oob_required, int page,
  454. int cached, int raw)
  455. {
  456. int status;
  457. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  458. if (unlikely(raw))
  459. chip->ecc.write_page_raw(mtd, chip, buf, oob_required);
  460. else
  461. chip->ecc.write_page(mtd, chip, buf, oob_required);
  462. /*
  463. * Cached progamming disabled for now, Not sure if its worth the
  464. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
  465. */
  466. cached = 0;
  467. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  468. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  469. status = chip->waitfunc(mtd, chip);
  470. /*
  471. * See if operation failed and additional status checks are
  472. * available
  473. */
  474. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  475. status = chip->errstat(mtd, chip, FL_WRITING, status,
  476. page);
  477. if (status & NAND_STATUS_FAIL)
  478. return -EIO;
  479. } else {
  480. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  481. status = chip->waitfunc(mtd, chip);
  482. }
  483. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  484. /* Send command to read back the data */
  485. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  486. if (chip->verify_buf(mtd, buf, mtd->writesize))
  487. return -EIO;
  488. #endif
  489. return 0;
  490. }
  491. static int cafe_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  492. {
  493. return 0;
  494. }
  495. /* F_2[X]/(X**6+X+1) */
  496. static unsigned short __devinit gf64_mul(u8 a, u8 b)
  497. {
  498. u8 c;
  499. unsigned int i;
  500. c = 0;
  501. for (i = 0; i < 6; i++) {
  502. if (a & 1)
  503. c ^= b;
  504. a >>= 1;
  505. b <<= 1;
  506. if ((b & 0x40) != 0)
  507. b ^= 0x43;
  508. }
  509. return c;
  510. }
  511. /* F_64[X]/(X**2+X+A**-1) with A the generator of F_64[X] */
  512. static u16 __devinit gf4096_mul(u16 a, u16 b)
  513. {
  514. u8 ah, al, bh, bl, ch, cl;
  515. ah = a >> 6;
  516. al = a & 0x3f;
  517. bh = b >> 6;
  518. bl = b & 0x3f;
  519. ch = gf64_mul(ah ^ al, bh ^ bl) ^ gf64_mul(al, bl);
  520. cl = gf64_mul(gf64_mul(ah, bh), 0x21) ^ gf64_mul(al, bl);
  521. return (ch << 6) ^ cl;
  522. }
  523. static int __devinit cafe_mul(int x)
  524. {
  525. if (x == 0)
  526. return 1;
  527. return gf4096_mul(x, 0xe01);
  528. }
  529. static int __devinit cafe_nand_probe(struct pci_dev *pdev,
  530. const struct pci_device_id *ent)
  531. {
  532. struct mtd_info *mtd;
  533. struct cafe_priv *cafe;
  534. uint32_t ctrl;
  535. int err = 0;
  536. /* Very old versions shared the same PCI ident for all three
  537. functions on the chip. Verify the class too... */
  538. if ((pdev->class >> 8) != PCI_CLASS_MEMORY_FLASH)
  539. return -ENODEV;
  540. err = pci_enable_device(pdev);
  541. if (err)
  542. return err;
  543. pci_set_master(pdev);
  544. mtd = kzalloc(sizeof(*mtd) + sizeof(struct cafe_priv), GFP_KERNEL);
  545. if (!mtd) {
  546. dev_warn(&pdev->dev, "failed to alloc mtd_info\n");
  547. return -ENOMEM;
  548. }
  549. cafe = (void *)(&mtd[1]);
  550. mtd->dev.parent = &pdev->dev;
  551. mtd->priv = cafe;
  552. mtd->owner = THIS_MODULE;
  553. cafe->pdev = pdev;
  554. cafe->mmio = pci_iomap(pdev, 0, 0);
  555. if (!cafe->mmio) {
  556. dev_warn(&pdev->dev, "failed to iomap\n");
  557. err = -ENOMEM;
  558. goto out_free_mtd;
  559. }
  560. cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
  561. &cafe->dmaaddr, GFP_KERNEL);
  562. if (!cafe->dmabuf) {
  563. err = -ENOMEM;
  564. goto out_ior;
  565. }
  566. cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
  567. cafe->rs = init_rs_non_canonical(12, &cafe_mul, 0, 1, 8);
  568. if (!cafe->rs) {
  569. err = -ENOMEM;
  570. goto out_ior;
  571. }
  572. cafe->nand.cmdfunc = cafe_nand_cmdfunc;
  573. cafe->nand.dev_ready = cafe_device_ready;
  574. cafe->nand.read_byte = cafe_read_byte;
  575. cafe->nand.read_buf = cafe_read_buf;
  576. cafe->nand.write_buf = cafe_write_buf;
  577. cafe->nand.select_chip = cafe_select_chip;
  578. cafe->nand.chip_delay = 0;
  579. /* Enable the following for a flash based bad block table */
  580. cafe->nand.bbt_options = NAND_BBT_USE_FLASH;
  581. cafe->nand.options = NAND_OWN_BUFFERS;
  582. if (skipbbt) {
  583. cafe->nand.options |= NAND_SKIP_BBTSCAN;
  584. cafe->nand.block_bad = cafe_nand_block_bad;
  585. }
  586. if (numtimings && numtimings != 3) {
  587. dev_warn(&cafe->pdev->dev, "%d timing register values ignored; precisely three are required\n", numtimings);
  588. }
  589. if (numtimings == 3) {
  590. cafe_dev_dbg(&cafe->pdev->dev, "Using provided timings (%08x %08x %08x)\n",
  591. timing[0], timing[1], timing[2]);
  592. } else {
  593. timing[0] = cafe_readl(cafe, NAND_TIMING1);
  594. timing[1] = cafe_readl(cafe, NAND_TIMING2);
  595. timing[2] = cafe_readl(cafe, NAND_TIMING3);
  596. if (timing[0] | timing[1] | timing[2]) {
  597. cafe_dev_dbg(&cafe->pdev->dev, "Timing registers already set (%08x %08x %08x)\n",
  598. timing[0], timing[1], timing[2]);
  599. } else {
  600. dev_warn(&cafe->pdev->dev, "Timing registers unset; using most conservative defaults\n");
  601. timing[0] = timing[1] = timing[2] = 0xffffffff;
  602. }
  603. }
  604. /* Start off by resetting the NAND controller completely */
  605. cafe_writel(cafe, 1, NAND_RESET);
  606. cafe_writel(cafe, 0, NAND_RESET);
  607. cafe_writel(cafe, timing[0], NAND_TIMING1);
  608. cafe_writel(cafe, timing[1], NAND_TIMING2);
  609. cafe_writel(cafe, timing[2], NAND_TIMING3);
  610. cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
  611. err = request_irq(pdev->irq, &cafe_nand_interrupt, IRQF_SHARED,
  612. "CAFE NAND", mtd);
  613. if (err) {
  614. dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
  615. goto out_free_dma;
  616. }
  617. /* Disable master reset, enable NAND clock */
  618. ctrl = cafe_readl(cafe, GLOBAL_CTRL);
  619. ctrl &= 0xffffeff0;
  620. ctrl |= 0x00007000;
  621. cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
  622. cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
  623. cafe_writel(cafe, 0, NAND_DMA_CTRL);
  624. cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
  625. cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
  626. /* Set up DMA address */
  627. cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
  628. if (sizeof(cafe->dmaaddr) > 4)
  629. /* Shift in two parts to shut the compiler up */
  630. cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
  631. else
  632. cafe_writel(cafe, 0, NAND_DMA_ADDR1);
  633. cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
  634. cafe_readl(cafe, NAND_DMA_ADDR0), cafe->dmabuf);
  635. /* Enable NAND IRQ in global IRQ mask register */
  636. cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
  637. cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
  638. cafe_readl(cafe, GLOBAL_CTRL), cafe_readl(cafe, GLOBAL_IRQ_MASK));
  639. /* Scan to find existence of the device */
  640. if (nand_scan_ident(mtd, 2, NULL)) {
  641. err = -ENXIO;
  642. goto out_irq;
  643. }
  644. cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
  645. if (mtd->writesize == 2048)
  646. cafe->ctl2 |= 1<<29; /* 2KiB page size */
  647. /* Set up ECC according to the type of chip we found */
  648. if (mtd->writesize == 2048) {
  649. cafe->nand.ecc.layout = &cafe_oobinfo_2048;
  650. cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
  651. cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
  652. } else if (mtd->writesize == 512) {
  653. cafe->nand.ecc.layout = &cafe_oobinfo_512;
  654. cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
  655. cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
  656. } else {
  657. printk(KERN_WARNING "Unexpected NAND flash writesize %d. Aborting\n",
  658. mtd->writesize);
  659. goto out_irq;
  660. }
  661. cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
  662. cafe->nand.ecc.size = mtd->writesize;
  663. cafe->nand.ecc.bytes = 14;
  664. cafe->nand.ecc.strength = 4;
  665. cafe->nand.ecc.hwctl = (void *)cafe_nand_bug;
  666. cafe->nand.ecc.calculate = (void *)cafe_nand_bug;
  667. cafe->nand.ecc.correct = (void *)cafe_nand_bug;
  668. cafe->nand.write_page = cafe_nand_write_page;
  669. cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
  670. cafe->nand.ecc.write_oob = cafe_nand_write_oob;
  671. cafe->nand.ecc.read_page = cafe_nand_read_page;
  672. cafe->nand.ecc.read_oob = cafe_nand_read_oob;
  673. err = nand_scan_tail(mtd);
  674. if (err)
  675. goto out_irq;
  676. pci_set_drvdata(pdev, mtd);
  677. mtd->name = "cafe_nand";
  678. mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
  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. /* Disable NAND IRQ in global IRQ mask register */
  698. cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
  699. free_irq(pdev->irq, mtd);
  700. nand_release(mtd);
  701. free_rs(cafe->rs);
  702. pci_iounmap(pdev, cafe->mmio);
  703. dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
  704. kfree(mtd);
  705. }
  706. static const struct pci_device_id cafe_nand_tbl[] = {
  707. { PCI_VENDOR_ID_MARVELL, PCI_DEVICE_ID_MARVELL_88ALP01_NAND,
  708. 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. module_pci_driver(cafe_nand_pci_driver);
  753. MODULE_LICENSE("GPL");
  754. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  755. MODULE_DESCRIPTION("NAND flash driver for OLPC CAFÉ chip");