cafe_nand.c 24 KB

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