omap2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. * linux/drivers/mtd/onenand/omap2.c
  3. *
  4. * OneNAND driver for OMAP2 / OMAP3
  5. *
  6. * Copyright © 2005-2006 Nokia Corporation
  7. *
  8. * Author: Jarkko Lavinen <jarkko.lavinen@nokia.com> and Juha Yrjölä
  9. * IRQ and DMA support written by Timo Teras
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License version 2 as published by
  13. * the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with
  21. * this program; see the file COPYING. If not, write to the Free Software
  22. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. *
  24. */
  25. #include <linux/device.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/mtd/mtd.h>
  29. #include <linux/mtd/onenand.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/delay.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/io.h>
  36. #include <linux/slab.h>
  37. #include <linux/regulator/consumer.h>
  38. #include <asm/mach/flash.h>
  39. #include <plat/gpmc.h>
  40. #include <plat/onenand.h>
  41. #include <asm/gpio.h>
  42. #include <plat/dma.h>
  43. #include <plat/board.h>
  44. #define DRIVER_NAME "omap2-onenand"
  45. #define ONENAND_IO_SIZE SZ_128K
  46. #define ONENAND_BUFRAM_SIZE (1024 * 5)
  47. struct omap2_onenand {
  48. struct platform_device *pdev;
  49. int gpmc_cs;
  50. unsigned long phys_base;
  51. int gpio_irq;
  52. struct mtd_info mtd;
  53. struct onenand_chip onenand;
  54. struct completion irq_done;
  55. struct completion dma_done;
  56. int dma_channel;
  57. int freq;
  58. int (*setup)(void __iomem *base, int *freq_ptr);
  59. struct regulator *regulator;
  60. };
  61. static void omap2_onenand_dma_cb(int lch, u16 ch_status, void *data)
  62. {
  63. struct omap2_onenand *c = data;
  64. complete(&c->dma_done);
  65. }
  66. static irqreturn_t omap2_onenand_interrupt(int irq, void *dev_id)
  67. {
  68. struct omap2_onenand *c = dev_id;
  69. complete(&c->irq_done);
  70. return IRQ_HANDLED;
  71. }
  72. static inline unsigned short read_reg(struct omap2_onenand *c, int reg)
  73. {
  74. return readw(c->onenand.base + reg);
  75. }
  76. static inline void write_reg(struct omap2_onenand *c, unsigned short value,
  77. int reg)
  78. {
  79. writew(value, c->onenand.base + reg);
  80. }
  81. static void wait_err(char *msg, int state, unsigned int ctrl, unsigned int intr)
  82. {
  83. printk(KERN_ERR "onenand_wait: %s! state %d ctrl 0x%04x intr 0x%04x\n",
  84. msg, state, ctrl, intr);
  85. }
  86. static void wait_warn(char *msg, int state, unsigned int ctrl,
  87. unsigned int intr)
  88. {
  89. printk(KERN_WARNING "onenand_wait: %s! state %d ctrl 0x%04x "
  90. "intr 0x%04x\n", msg, state, ctrl, intr);
  91. }
  92. static int omap2_onenand_wait(struct mtd_info *mtd, int state)
  93. {
  94. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  95. struct onenand_chip *this = mtd->priv;
  96. unsigned int intr = 0;
  97. unsigned int ctrl, ctrl_mask;
  98. unsigned long timeout;
  99. u32 syscfg;
  100. if (state == FL_RESETING || state == FL_PREPARING_ERASE ||
  101. state == FL_VERIFYING_ERASE) {
  102. int i = 21;
  103. unsigned int intr_flags = ONENAND_INT_MASTER;
  104. switch (state) {
  105. case FL_RESETING:
  106. intr_flags |= ONENAND_INT_RESET;
  107. break;
  108. case FL_PREPARING_ERASE:
  109. intr_flags |= ONENAND_INT_ERASE;
  110. break;
  111. case FL_VERIFYING_ERASE:
  112. i = 101;
  113. break;
  114. }
  115. while (--i) {
  116. udelay(1);
  117. intr = read_reg(c, ONENAND_REG_INTERRUPT);
  118. if (intr & ONENAND_INT_MASTER)
  119. break;
  120. }
  121. ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
  122. if (ctrl & ONENAND_CTRL_ERROR) {
  123. wait_err("controller error", state, ctrl, intr);
  124. return -EIO;
  125. }
  126. if ((intr & intr_flags) == intr_flags)
  127. return 0;
  128. /* Continue in wait for interrupt branch */
  129. }
  130. if (state != FL_READING) {
  131. int result;
  132. /* Turn interrupts on */
  133. syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
  134. if (!(syscfg & ONENAND_SYS_CFG1_IOBE)) {
  135. syscfg |= ONENAND_SYS_CFG1_IOBE;
  136. write_reg(c, syscfg, ONENAND_REG_SYS_CFG1);
  137. if (cpu_is_omap34xx())
  138. /* Add a delay to let GPIO settle */
  139. syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
  140. }
  141. INIT_COMPLETION(c->irq_done);
  142. if (c->gpio_irq) {
  143. result = gpio_get_value(c->gpio_irq);
  144. if (result == -1) {
  145. ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
  146. intr = read_reg(c, ONENAND_REG_INTERRUPT);
  147. wait_err("gpio error", state, ctrl, intr);
  148. return -EIO;
  149. }
  150. } else
  151. result = 0;
  152. if (result == 0) {
  153. int retry_cnt = 0;
  154. retry:
  155. result = wait_for_completion_timeout(&c->irq_done,
  156. msecs_to_jiffies(20));
  157. if (result == 0) {
  158. /* Timeout after 20ms */
  159. ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
  160. if (ctrl & ONENAND_CTRL_ONGO &&
  161. !this->ongoing) {
  162. /*
  163. * The operation seems to be still going
  164. * so give it some more time.
  165. */
  166. retry_cnt += 1;
  167. if (retry_cnt < 3)
  168. goto retry;
  169. intr = read_reg(c,
  170. ONENAND_REG_INTERRUPT);
  171. wait_err("timeout", state, ctrl, intr);
  172. return -EIO;
  173. }
  174. intr = read_reg(c, ONENAND_REG_INTERRUPT);
  175. if ((intr & ONENAND_INT_MASTER) == 0)
  176. wait_warn("timeout", state, ctrl, intr);
  177. }
  178. }
  179. } else {
  180. int retry_cnt = 0;
  181. /* Turn interrupts off */
  182. syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
  183. syscfg &= ~ONENAND_SYS_CFG1_IOBE;
  184. write_reg(c, syscfg, ONENAND_REG_SYS_CFG1);
  185. timeout = jiffies + msecs_to_jiffies(20);
  186. while (1) {
  187. if (time_before(jiffies, timeout)) {
  188. intr = read_reg(c, ONENAND_REG_INTERRUPT);
  189. if (intr & ONENAND_INT_MASTER)
  190. break;
  191. } else {
  192. /* Timeout after 20ms */
  193. ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
  194. if (ctrl & ONENAND_CTRL_ONGO) {
  195. /*
  196. * The operation seems to be still going
  197. * so give it some more time.
  198. */
  199. retry_cnt += 1;
  200. if (retry_cnt < 3) {
  201. timeout = jiffies +
  202. msecs_to_jiffies(20);
  203. continue;
  204. }
  205. }
  206. break;
  207. }
  208. }
  209. }
  210. intr = read_reg(c, ONENAND_REG_INTERRUPT);
  211. ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
  212. if (intr & ONENAND_INT_READ) {
  213. int ecc = read_reg(c, ONENAND_REG_ECC_STATUS);
  214. if (ecc) {
  215. unsigned int addr1, addr8;
  216. addr1 = read_reg(c, ONENAND_REG_START_ADDRESS1);
  217. addr8 = read_reg(c, ONENAND_REG_START_ADDRESS8);
  218. if (ecc & ONENAND_ECC_2BIT_ALL) {
  219. printk(KERN_ERR "onenand_wait: ECC error = "
  220. "0x%04x, addr1 %#x, addr8 %#x\n",
  221. ecc, addr1, addr8);
  222. mtd->ecc_stats.failed++;
  223. return -EBADMSG;
  224. } else if (ecc & ONENAND_ECC_1BIT_ALL) {
  225. printk(KERN_NOTICE "onenand_wait: correctable "
  226. "ECC error = 0x%04x, addr1 %#x, "
  227. "addr8 %#x\n", ecc, addr1, addr8);
  228. mtd->ecc_stats.corrected++;
  229. }
  230. }
  231. } else if (state == FL_READING) {
  232. wait_err("timeout", state, ctrl, intr);
  233. return -EIO;
  234. }
  235. if (ctrl & ONENAND_CTRL_ERROR) {
  236. wait_err("controller error", state, ctrl, intr);
  237. if (ctrl & ONENAND_CTRL_LOCK)
  238. printk(KERN_ERR "onenand_wait: "
  239. "Device is write protected!!!\n");
  240. return -EIO;
  241. }
  242. ctrl_mask = 0xFE9F;
  243. if (this->ongoing)
  244. ctrl_mask &= ~0x8000;
  245. if (ctrl & ctrl_mask)
  246. wait_warn("unexpected controller status", state, ctrl, intr);
  247. return 0;
  248. }
  249. static inline int omap2_onenand_bufferram_offset(struct mtd_info *mtd, int area)
  250. {
  251. struct onenand_chip *this = mtd->priv;
  252. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  253. if (area == ONENAND_DATARAM)
  254. return this->writesize;
  255. if (area == ONENAND_SPARERAM)
  256. return mtd->oobsize;
  257. }
  258. return 0;
  259. }
  260. #if defined(CONFIG_ARCH_OMAP3) || defined(MULTI_OMAP2)
  261. static int omap3_onenand_read_bufferram(struct mtd_info *mtd, int area,
  262. unsigned char *buffer, int offset,
  263. size_t count)
  264. {
  265. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  266. struct onenand_chip *this = mtd->priv;
  267. dma_addr_t dma_src, dma_dst;
  268. int bram_offset;
  269. unsigned long timeout;
  270. void *buf = (void *)buffer;
  271. size_t xtra;
  272. volatile unsigned *done;
  273. bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
  274. if (bram_offset & 3 || (size_t)buf & 3 || count < 384)
  275. goto out_copy;
  276. /* panic_write() may be in an interrupt context */
  277. if (in_interrupt() || oops_in_progress)
  278. goto out_copy;
  279. if (buf >= high_memory) {
  280. struct page *p1;
  281. if (((size_t)buf & PAGE_MASK) !=
  282. ((size_t)(buf + count - 1) & PAGE_MASK))
  283. goto out_copy;
  284. p1 = vmalloc_to_page(buf);
  285. if (!p1)
  286. goto out_copy;
  287. buf = page_address(p1) + ((size_t)buf & ~PAGE_MASK);
  288. }
  289. xtra = count & 3;
  290. if (xtra) {
  291. count -= xtra;
  292. memcpy(buf + count, this->base + bram_offset + count, xtra);
  293. }
  294. dma_src = c->phys_base + bram_offset;
  295. dma_dst = dma_map_single(&c->pdev->dev, buf, count, DMA_FROM_DEVICE);
  296. if (dma_mapping_error(&c->pdev->dev, dma_dst)) {
  297. dev_err(&c->pdev->dev,
  298. "Couldn't DMA map a %d byte buffer\n",
  299. count);
  300. goto out_copy;
  301. }
  302. omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S32,
  303. count >> 2, 1, 0, 0, 0);
  304. omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  305. dma_src, 0, 0);
  306. omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  307. dma_dst, 0, 0);
  308. INIT_COMPLETION(c->dma_done);
  309. omap_start_dma(c->dma_channel);
  310. timeout = jiffies + msecs_to_jiffies(20);
  311. done = &c->dma_done.done;
  312. while (time_before(jiffies, timeout))
  313. if (*done)
  314. break;
  315. dma_unmap_single(&c->pdev->dev, dma_dst, count, DMA_FROM_DEVICE);
  316. if (!*done) {
  317. dev_err(&c->pdev->dev, "timeout waiting for DMA\n");
  318. goto out_copy;
  319. }
  320. return 0;
  321. out_copy:
  322. memcpy(buf, this->base + bram_offset, count);
  323. return 0;
  324. }
  325. static int omap3_onenand_write_bufferram(struct mtd_info *mtd, int area,
  326. const unsigned char *buffer,
  327. int offset, size_t count)
  328. {
  329. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  330. struct onenand_chip *this = mtd->priv;
  331. dma_addr_t dma_src, dma_dst;
  332. int bram_offset;
  333. unsigned long timeout;
  334. void *buf = (void *)buffer;
  335. volatile unsigned *done;
  336. bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
  337. if (bram_offset & 3 || (size_t)buf & 3 || count < 384)
  338. goto out_copy;
  339. /* panic_write() may be in an interrupt context */
  340. if (in_interrupt() || oops_in_progress)
  341. goto out_copy;
  342. if (buf >= high_memory) {
  343. struct page *p1;
  344. if (((size_t)buf & PAGE_MASK) !=
  345. ((size_t)(buf + count - 1) & PAGE_MASK))
  346. goto out_copy;
  347. p1 = vmalloc_to_page(buf);
  348. if (!p1)
  349. goto out_copy;
  350. buf = page_address(p1) + ((size_t)buf & ~PAGE_MASK);
  351. }
  352. dma_src = dma_map_single(&c->pdev->dev, buf, count, DMA_TO_DEVICE);
  353. dma_dst = c->phys_base + bram_offset;
  354. if (dma_mapping_error(&c->pdev->dev, dma_src)) {
  355. dev_err(&c->pdev->dev,
  356. "Couldn't DMA map a %d byte buffer\n",
  357. count);
  358. return -1;
  359. }
  360. omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S32,
  361. count >> 2, 1, 0, 0, 0);
  362. omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  363. dma_src, 0, 0);
  364. omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  365. dma_dst, 0, 0);
  366. INIT_COMPLETION(c->dma_done);
  367. omap_start_dma(c->dma_channel);
  368. timeout = jiffies + msecs_to_jiffies(20);
  369. done = &c->dma_done.done;
  370. while (time_before(jiffies, timeout))
  371. if (*done)
  372. break;
  373. dma_unmap_single(&c->pdev->dev, dma_src, count, DMA_TO_DEVICE);
  374. if (!*done) {
  375. dev_err(&c->pdev->dev, "timeout waiting for DMA\n");
  376. goto out_copy;
  377. }
  378. return 0;
  379. out_copy:
  380. memcpy(this->base + bram_offset, buf, count);
  381. return 0;
  382. }
  383. #else
  384. int omap3_onenand_read_bufferram(struct mtd_info *mtd, int area,
  385. unsigned char *buffer, int offset,
  386. size_t count);
  387. int omap3_onenand_write_bufferram(struct mtd_info *mtd, int area,
  388. const unsigned char *buffer,
  389. int offset, size_t count);
  390. #endif
  391. #if defined(CONFIG_ARCH_OMAP2) || defined(MULTI_OMAP2)
  392. static int omap2_onenand_read_bufferram(struct mtd_info *mtd, int area,
  393. unsigned char *buffer, int offset,
  394. size_t count)
  395. {
  396. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  397. struct onenand_chip *this = mtd->priv;
  398. dma_addr_t dma_src, dma_dst;
  399. int bram_offset;
  400. bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
  401. /* DMA is not used. Revisit PM requirements before enabling it. */
  402. if (1 || (c->dma_channel < 0) ||
  403. ((void *) buffer >= (void *) high_memory) || (bram_offset & 3) ||
  404. (((unsigned int) buffer) & 3) || (count < 1024) || (count & 3)) {
  405. memcpy(buffer, (__force void *)(this->base + bram_offset),
  406. count);
  407. return 0;
  408. }
  409. dma_src = c->phys_base + bram_offset;
  410. dma_dst = dma_map_single(&c->pdev->dev, buffer, count,
  411. DMA_FROM_DEVICE);
  412. if (dma_mapping_error(&c->pdev->dev, dma_dst)) {
  413. dev_err(&c->pdev->dev,
  414. "Couldn't DMA map a %d byte buffer\n",
  415. count);
  416. return -1;
  417. }
  418. omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S32,
  419. count / 4, 1, 0, 0, 0);
  420. omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  421. dma_src, 0, 0);
  422. omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  423. dma_dst, 0, 0);
  424. INIT_COMPLETION(c->dma_done);
  425. omap_start_dma(c->dma_channel);
  426. wait_for_completion(&c->dma_done);
  427. dma_unmap_single(&c->pdev->dev, dma_dst, count, DMA_FROM_DEVICE);
  428. return 0;
  429. }
  430. static int omap2_onenand_write_bufferram(struct mtd_info *mtd, int area,
  431. const unsigned char *buffer,
  432. int offset, size_t count)
  433. {
  434. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  435. struct onenand_chip *this = mtd->priv;
  436. dma_addr_t dma_src, dma_dst;
  437. int bram_offset;
  438. bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
  439. /* DMA is not used. Revisit PM requirements before enabling it. */
  440. if (1 || (c->dma_channel < 0) ||
  441. ((void *) buffer >= (void *) high_memory) || (bram_offset & 3) ||
  442. (((unsigned int) buffer) & 3) || (count < 1024) || (count & 3)) {
  443. memcpy((__force void *)(this->base + bram_offset), buffer,
  444. count);
  445. return 0;
  446. }
  447. dma_src = dma_map_single(&c->pdev->dev, (void *) buffer, count,
  448. DMA_TO_DEVICE);
  449. dma_dst = c->phys_base + bram_offset;
  450. if (dma_mapping_error(&c->pdev->dev, dma_src)) {
  451. dev_err(&c->pdev->dev,
  452. "Couldn't DMA map a %d byte buffer\n",
  453. count);
  454. return -1;
  455. }
  456. omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S16,
  457. count / 2, 1, 0, 0, 0);
  458. omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  459. dma_src, 0, 0);
  460. omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  461. dma_dst, 0, 0);
  462. INIT_COMPLETION(c->dma_done);
  463. omap_start_dma(c->dma_channel);
  464. wait_for_completion(&c->dma_done);
  465. dma_unmap_single(&c->pdev->dev, dma_src, count, DMA_TO_DEVICE);
  466. return 0;
  467. }
  468. #else
  469. int omap2_onenand_read_bufferram(struct mtd_info *mtd, int area,
  470. unsigned char *buffer, int offset,
  471. size_t count);
  472. int omap2_onenand_write_bufferram(struct mtd_info *mtd, int area,
  473. const unsigned char *buffer,
  474. int offset, size_t count);
  475. #endif
  476. static struct platform_driver omap2_onenand_driver;
  477. static int __adjust_timing(struct device *dev, void *data)
  478. {
  479. int ret = 0;
  480. struct omap2_onenand *c;
  481. c = dev_get_drvdata(dev);
  482. BUG_ON(c->setup == NULL);
  483. /* DMA is not in use so this is all that is needed */
  484. /* Revisit for OMAP3! */
  485. ret = c->setup(c->onenand.base, &c->freq);
  486. return ret;
  487. }
  488. int omap2_onenand_rephase(void)
  489. {
  490. return driver_for_each_device(&omap2_onenand_driver.driver, NULL,
  491. NULL, __adjust_timing);
  492. }
  493. static void omap2_onenand_shutdown(struct platform_device *pdev)
  494. {
  495. struct omap2_onenand *c = dev_get_drvdata(&pdev->dev);
  496. /* With certain content in the buffer RAM, the OMAP boot ROM code
  497. * can recognize the flash chip incorrectly. Zero it out before
  498. * soft reset.
  499. */
  500. memset((__force void *)c->onenand.base, 0, ONENAND_BUFRAM_SIZE);
  501. }
  502. static int omap2_onenand_enable(struct mtd_info *mtd)
  503. {
  504. int ret;
  505. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  506. ret = regulator_enable(c->regulator);
  507. if (ret != 0)
  508. dev_err(&c->pdev->dev, "can't enable regulator\n");
  509. return ret;
  510. }
  511. static int omap2_onenand_disable(struct mtd_info *mtd)
  512. {
  513. int ret;
  514. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  515. ret = regulator_disable(c->regulator);
  516. if (ret != 0)
  517. dev_err(&c->pdev->dev, "can't disable regulator\n");
  518. return ret;
  519. }
  520. static int __devinit omap2_onenand_probe(struct platform_device *pdev)
  521. {
  522. struct omap_onenand_platform_data *pdata;
  523. struct omap2_onenand *c;
  524. struct onenand_chip *this;
  525. int r;
  526. pdata = pdev->dev.platform_data;
  527. if (pdata == NULL) {
  528. dev_err(&pdev->dev, "platform data missing\n");
  529. return -ENODEV;
  530. }
  531. c = kzalloc(sizeof(struct omap2_onenand), GFP_KERNEL);
  532. if (!c)
  533. return -ENOMEM;
  534. init_completion(&c->irq_done);
  535. init_completion(&c->dma_done);
  536. c->gpmc_cs = pdata->cs;
  537. c->gpio_irq = pdata->gpio_irq;
  538. c->dma_channel = pdata->dma_channel;
  539. if (c->dma_channel < 0) {
  540. /* if -1, don't use DMA */
  541. c->gpio_irq = 0;
  542. }
  543. r = gpmc_cs_request(c->gpmc_cs, ONENAND_IO_SIZE, &c->phys_base);
  544. if (r < 0) {
  545. dev_err(&pdev->dev, "Cannot request GPMC CS\n");
  546. goto err_kfree;
  547. }
  548. if (request_mem_region(c->phys_base, ONENAND_IO_SIZE,
  549. pdev->dev.driver->name) == NULL) {
  550. dev_err(&pdev->dev, "Cannot reserve memory region at 0x%08lx, "
  551. "size: 0x%x\n", c->phys_base, ONENAND_IO_SIZE);
  552. r = -EBUSY;
  553. goto err_free_cs;
  554. }
  555. c->onenand.base = ioremap(c->phys_base, ONENAND_IO_SIZE);
  556. if (c->onenand.base == NULL) {
  557. r = -ENOMEM;
  558. goto err_release_mem_region;
  559. }
  560. if (pdata->onenand_setup != NULL) {
  561. r = pdata->onenand_setup(c->onenand.base, &c->freq);
  562. if (r < 0) {
  563. dev_err(&pdev->dev, "Onenand platform setup failed: "
  564. "%d\n", r);
  565. goto err_iounmap;
  566. }
  567. c->setup = pdata->onenand_setup;
  568. }
  569. if (c->gpio_irq) {
  570. if ((r = gpio_request(c->gpio_irq, "OneNAND irq")) < 0) {
  571. dev_err(&pdev->dev, "Failed to request GPIO%d for "
  572. "OneNAND\n", c->gpio_irq);
  573. goto err_iounmap;
  574. }
  575. gpio_direction_input(c->gpio_irq);
  576. if ((r = request_irq(gpio_to_irq(c->gpio_irq),
  577. omap2_onenand_interrupt, IRQF_TRIGGER_RISING,
  578. pdev->dev.driver->name, c)) < 0)
  579. goto err_release_gpio;
  580. }
  581. if (c->dma_channel >= 0) {
  582. r = omap_request_dma(0, pdev->dev.driver->name,
  583. omap2_onenand_dma_cb, (void *) c,
  584. &c->dma_channel);
  585. if (r == 0) {
  586. omap_set_dma_write_mode(c->dma_channel,
  587. OMAP_DMA_WRITE_NON_POSTED);
  588. omap_set_dma_src_data_pack(c->dma_channel, 1);
  589. omap_set_dma_src_burst_mode(c->dma_channel,
  590. OMAP_DMA_DATA_BURST_8);
  591. omap_set_dma_dest_data_pack(c->dma_channel, 1);
  592. omap_set_dma_dest_burst_mode(c->dma_channel,
  593. OMAP_DMA_DATA_BURST_8);
  594. } else {
  595. dev_info(&pdev->dev,
  596. "failed to allocate DMA for OneNAND, "
  597. "using PIO instead\n");
  598. c->dma_channel = -1;
  599. }
  600. }
  601. dev_info(&pdev->dev, "initializing on CS%d, phys base 0x%08lx, virtual "
  602. "base %p, freq %d MHz\n", c->gpmc_cs, c->phys_base,
  603. c->onenand.base, c->freq);
  604. c->pdev = pdev;
  605. c->mtd.name = dev_name(&pdev->dev);
  606. c->mtd.priv = &c->onenand;
  607. c->mtd.owner = THIS_MODULE;
  608. c->mtd.dev.parent = &pdev->dev;
  609. this = &c->onenand;
  610. if (c->dma_channel >= 0) {
  611. this->wait = omap2_onenand_wait;
  612. if (cpu_is_omap34xx()) {
  613. this->read_bufferram = omap3_onenand_read_bufferram;
  614. this->write_bufferram = omap3_onenand_write_bufferram;
  615. } else {
  616. this->read_bufferram = omap2_onenand_read_bufferram;
  617. this->write_bufferram = omap2_onenand_write_bufferram;
  618. }
  619. }
  620. if (pdata->regulator_can_sleep) {
  621. c->regulator = regulator_get(&pdev->dev, "vonenand");
  622. if (IS_ERR(c->regulator)) {
  623. dev_err(&pdev->dev, "Failed to get regulator\n");
  624. r = PTR_ERR(c->regulator);
  625. goto err_release_dma;
  626. }
  627. c->onenand.enable = omap2_onenand_enable;
  628. c->onenand.disable = omap2_onenand_disable;
  629. }
  630. if (pdata->skip_initial_unlocking)
  631. this->options |= ONENAND_SKIP_INITIAL_UNLOCKING;
  632. if ((r = onenand_scan(&c->mtd, 1)) < 0)
  633. goto err_release_regulator;
  634. r = mtd_device_parse_register(&c->mtd, NULL, NULL,
  635. pdata ? pdata->parts : NULL,
  636. pdata ? pdata->nr_parts : 0);
  637. if (r)
  638. goto err_release_onenand;
  639. platform_set_drvdata(pdev, c);
  640. return 0;
  641. err_release_onenand:
  642. onenand_release(&c->mtd);
  643. err_release_regulator:
  644. regulator_put(c->regulator);
  645. err_release_dma:
  646. if (c->dma_channel != -1)
  647. omap_free_dma(c->dma_channel);
  648. if (c->gpio_irq)
  649. free_irq(gpio_to_irq(c->gpio_irq), c);
  650. err_release_gpio:
  651. if (c->gpio_irq)
  652. gpio_free(c->gpio_irq);
  653. err_iounmap:
  654. iounmap(c->onenand.base);
  655. err_release_mem_region:
  656. release_mem_region(c->phys_base, ONENAND_IO_SIZE);
  657. err_free_cs:
  658. gpmc_cs_free(c->gpmc_cs);
  659. err_kfree:
  660. kfree(c);
  661. return r;
  662. }
  663. static int __devexit omap2_onenand_remove(struct platform_device *pdev)
  664. {
  665. struct omap2_onenand *c = dev_get_drvdata(&pdev->dev);
  666. onenand_release(&c->mtd);
  667. regulator_put(c->regulator);
  668. if (c->dma_channel != -1)
  669. omap_free_dma(c->dma_channel);
  670. omap2_onenand_shutdown(pdev);
  671. platform_set_drvdata(pdev, NULL);
  672. if (c->gpio_irq) {
  673. free_irq(gpio_to_irq(c->gpio_irq), c);
  674. gpio_free(c->gpio_irq);
  675. }
  676. iounmap(c->onenand.base);
  677. release_mem_region(c->phys_base, ONENAND_IO_SIZE);
  678. gpmc_cs_free(c->gpmc_cs);
  679. kfree(c);
  680. return 0;
  681. }
  682. static struct platform_driver omap2_onenand_driver = {
  683. .probe = omap2_onenand_probe,
  684. .remove = __devexit_p(omap2_onenand_remove),
  685. .shutdown = omap2_onenand_shutdown,
  686. .driver = {
  687. .name = DRIVER_NAME,
  688. .owner = THIS_MODULE,
  689. },
  690. };
  691. static int __init omap2_onenand_init(void)
  692. {
  693. printk(KERN_INFO "OneNAND driver initializing\n");
  694. return platform_driver_register(&omap2_onenand_driver);
  695. }
  696. static void __exit omap2_onenand_exit(void)
  697. {
  698. platform_driver_unregister(&omap2_onenand_driver);
  699. }
  700. module_init(omap2_onenand_init);
  701. module_exit(omap2_onenand_exit);
  702. MODULE_ALIAS("platform:" DRIVER_NAME);
  703. MODULE_LICENSE("GPL");
  704. MODULE_AUTHOR("Jarkko Lavinen <jarkko.lavinen@nokia.com>");
  705. MODULE_DESCRIPTION("Glue layer for OneNAND flash on OMAP2 / OMAP3");