spear_smi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*
  2. * SMI (Serial Memory Controller) device driver for Serial NOR Flash on
  3. * SPEAr platform
  4. * The serial nor interface is largely based on drivers/mtd/m25p80.c,
  5. * however the SPI interface has been replaced by SMI.
  6. *
  7. * Copyright © 2010 STMicroelectronics.
  8. * Ashish Priyadarshi
  9. * Shiraz Hashim <shiraz.hashim@st.com>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/err.h>
  19. #include <linux/errno.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/ioport.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/param.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/mtd/spear_smi.h>
  32. #include <linux/mutex.h>
  33. #include <linux/sched.h>
  34. #include <linux/slab.h>
  35. #include <linux/wait.h>
  36. #include <linux/of.h>
  37. #include <linux/of_address.h>
  38. /* SMI clock rate */
  39. #define SMI_MAX_CLOCK_FREQ 50000000 /* 50 MHz */
  40. /* MAX time out to safely come out of a erase or write busy conditions */
  41. #define SMI_PROBE_TIMEOUT (HZ / 10)
  42. #define SMI_MAX_TIME_OUT (3 * HZ)
  43. /* timeout for command completion */
  44. #define SMI_CMD_TIMEOUT (HZ / 10)
  45. /* registers of smi */
  46. #define SMI_CR1 0x0 /* SMI control register 1 */
  47. #define SMI_CR2 0x4 /* SMI control register 2 */
  48. #define SMI_SR 0x8 /* SMI status register */
  49. #define SMI_TR 0xC /* SMI transmit register */
  50. #define SMI_RR 0x10 /* SMI receive register */
  51. /* defines for control_reg 1 */
  52. #define BANK_EN (0xF << 0) /* enables all banks */
  53. #define DSEL_TIME (0x6 << 4) /* Deselect time 6 + 1 SMI_CK periods */
  54. #define SW_MODE (0x1 << 28) /* enables SW Mode */
  55. #define WB_MODE (0x1 << 29) /* Write Burst Mode */
  56. #define FAST_MODE (0x1 << 15) /* Fast Mode */
  57. #define HOLD1 (0x1 << 16) /* Clock Hold period selection */
  58. /* defines for control_reg 2 */
  59. #define SEND (0x1 << 7) /* Send data */
  60. #define TFIE (0x1 << 8) /* Transmission Flag Interrupt Enable */
  61. #define WCIE (0x1 << 9) /* Write Complete Interrupt Enable */
  62. #define RD_STATUS_REG (0x1 << 10) /* reads status reg */
  63. #define WE (0x1 << 11) /* Write Enable */
  64. #define TX_LEN_SHIFT 0
  65. #define RX_LEN_SHIFT 4
  66. #define BANK_SHIFT 12
  67. /* defines for status register */
  68. #define SR_WIP 0x1 /* Write in progress */
  69. #define SR_WEL 0x2 /* Write enable latch */
  70. #define SR_BP0 0x4 /* Block protect 0 */
  71. #define SR_BP1 0x8 /* Block protect 1 */
  72. #define SR_BP2 0x10 /* Block protect 2 */
  73. #define SR_SRWD 0x80 /* SR write protect */
  74. #define TFF 0x100 /* Transfer Finished Flag */
  75. #define WCF 0x200 /* Transfer Finished Flag */
  76. #define ERF1 0x400 /* Forbidden Write Request */
  77. #define ERF2 0x800 /* Forbidden Access */
  78. #define WM_SHIFT 12
  79. /* flash opcodes */
  80. #define OPCODE_RDID 0x9f /* Read JEDEC ID */
  81. /* Flash Device Ids maintenance section */
  82. /* data structure to maintain flash ids from different vendors */
  83. struct flash_device {
  84. char *name;
  85. u8 erase_cmd;
  86. u32 device_id;
  87. u32 pagesize;
  88. unsigned long sectorsize;
  89. unsigned long size_in_bytes;
  90. };
  91. #define FLASH_ID(n, es, id, psize, ssize, size) \
  92. { \
  93. .name = n, \
  94. .erase_cmd = es, \
  95. .device_id = id, \
  96. .pagesize = psize, \
  97. .sectorsize = ssize, \
  98. .size_in_bytes = size \
  99. }
  100. static struct flash_device flash_devices[] = {
  101. FLASH_ID("st m25p16" , 0xd8, 0x00152020, 0x100, 0x10000, 0x200000),
  102. FLASH_ID("st m25p32" , 0xd8, 0x00162020, 0x100, 0x10000, 0x400000),
  103. FLASH_ID("st m25p64" , 0xd8, 0x00172020, 0x100, 0x10000, 0x800000),
  104. FLASH_ID("st m25p128" , 0xd8, 0x00182020, 0x100, 0x40000, 0x1000000),
  105. FLASH_ID("st m25p05" , 0xd8, 0x00102020, 0x80 , 0x8000 , 0x10000),
  106. FLASH_ID("st m25p10" , 0xd8, 0x00112020, 0x80 , 0x8000 , 0x20000),
  107. FLASH_ID("st m25p20" , 0xd8, 0x00122020, 0x100, 0x10000, 0x40000),
  108. FLASH_ID("st m25p40" , 0xd8, 0x00132020, 0x100, 0x10000, 0x80000),
  109. FLASH_ID("st m25p80" , 0xd8, 0x00142020, 0x100, 0x10000, 0x100000),
  110. FLASH_ID("st m45pe10" , 0xd8, 0x00114020, 0x100, 0x10000, 0x20000),
  111. FLASH_ID("st m45pe20" , 0xd8, 0x00124020, 0x100, 0x10000, 0x40000),
  112. FLASH_ID("st m45pe40" , 0xd8, 0x00134020, 0x100, 0x10000, 0x80000),
  113. FLASH_ID("st m45pe80" , 0xd8, 0x00144020, 0x100, 0x10000, 0x100000),
  114. FLASH_ID("sp s25fl004" , 0xd8, 0x00120201, 0x100, 0x10000, 0x80000),
  115. FLASH_ID("sp s25fl008" , 0xd8, 0x00130201, 0x100, 0x10000, 0x100000),
  116. FLASH_ID("sp s25fl016" , 0xd8, 0x00140201, 0x100, 0x10000, 0x200000),
  117. FLASH_ID("sp s25fl032" , 0xd8, 0x00150201, 0x100, 0x10000, 0x400000),
  118. FLASH_ID("sp s25fl064" , 0xd8, 0x00160201, 0x100, 0x10000, 0x800000),
  119. FLASH_ID("atmel 25f512" , 0x52, 0x0065001F, 0x80 , 0x8000 , 0x10000),
  120. FLASH_ID("atmel 25f1024" , 0x52, 0x0060001F, 0x100, 0x8000 , 0x20000),
  121. FLASH_ID("atmel 25f2048" , 0x52, 0x0063001F, 0x100, 0x10000, 0x40000),
  122. FLASH_ID("atmel 25f4096" , 0x52, 0x0064001F, 0x100, 0x10000, 0x80000),
  123. FLASH_ID("atmel 25fs040" , 0xd7, 0x0004661F, 0x100, 0x10000, 0x80000),
  124. FLASH_ID("mac 25l512" , 0xd8, 0x001020C2, 0x010, 0x10000, 0x10000),
  125. FLASH_ID("mac 25l1005" , 0xd8, 0x001120C2, 0x010, 0x10000, 0x20000),
  126. FLASH_ID("mac 25l2005" , 0xd8, 0x001220C2, 0x010, 0x10000, 0x40000),
  127. FLASH_ID("mac 25l4005" , 0xd8, 0x001320C2, 0x010, 0x10000, 0x80000),
  128. FLASH_ID("mac 25l4005a" , 0xd8, 0x001320C2, 0x010, 0x10000, 0x80000),
  129. FLASH_ID("mac 25l8005" , 0xd8, 0x001420C2, 0x010, 0x10000, 0x100000),
  130. FLASH_ID("mac 25l1605" , 0xd8, 0x001520C2, 0x100, 0x10000, 0x200000),
  131. FLASH_ID("mac 25l1605a" , 0xd8, 0x001520C2, 0x010, 0x10000, 0x200000),
  132. FLASH_ID("mac 25l3205" , 0xd8, 0x001620C2, 0x100, 0x10000, 0x400000),
  133. FLASH_ID("mac 25l3205a" , 0xd8, 0x001620C2, 0x100, 0x10000, 0x400000),
  134. FLASH_ID("mac 25l6405" , 0xd8, 0x001720C2, 0x100, 0x10000, 0x800000),
  135. };
  136. /* Define spear specific structures */
  137. struct spear_snor_flash;
  138. /**
  139. * struct spear_smi - Structure for SMI Device
  140. *
  141. * @clk: functional clock
  142. * @status: current status register of SMI.
  143. * @clk_rate: functional clock rate of SMI (default: SMI_MAX_CLOCK_FREQ)
  144. * @lock: lock to prevent parallel access of SMI.
  145. * @io_base: base address for registers of SMI.
  146. * @pdev: platform device
  147. * @cmd_complete: queue to wait for command completion of NOR-flash.
  148. * @num_flashes: number of flashes actually present on board.
  149. * @flash: separate structure for each Serial NOR-flash attached to SMI.
  150. */
  151. struct spear_smi {
  152. struct clk *clk;
  153. u32 status;
  154. unsigned long clk_rate;
  155. struct mutex lock;
  156. void __iomem *io_base;
  157. struct platform_device *pdev;
  158. wait_queue_head_t cmd_complete;
  159. u32 num_flashes;
  160. struct spear_snor_flash *flash[MAX_NUM_FLASH_CHIP];
  161. };
  162. /**
  163. * struct spear_snor_flash - Structure for Serial NOR Flash
  164. *
  165. * @bank: Bank number(0, 1, 2, 3) for each NOR-flash.
  166. * @dev_id: Device ID of NOR-flash.
  167. * @lock: lock to manage flash read, write and erase operations
  168. * @mtd: MTD info for each NOR-flash.
  169. * @num_parts: Total number of partition in each bank of NOR-flash.
  170. * @parts: Partition info for each bank of NOR-flash.
  171. * @page_size: Page size of NOR-flash.
  172. * @base_addr: Base address of NOR-flash.
  173. * @erase_cmd: erase command may vary on different flash types
  174. * @fast_mode: flash supports read in fast mode
  175. */
  176. struct spear_snor_flash {
  177. u32 bank;
  178. u32 dev_id;
  179. struct mutex lock;
  180. struct mtd_info mtd;
  181. u32 num_parts;
  182. struct mtd_partition *parts;
  183. u32 page_size;
  184. void __iomem *base_addr;
  185. u8 erase_cmd;
  186. u8 fast_mode;
  187. };
  188. static inline struct spear_snor_flash *get_flash_data(struct mtd_info *mtd)
  189. {
  190. return container_of(mtd, struct spear_snor_flash, mtd);
  191. }
  192. /**
  193. * spear_smi_read_sr - Read status register of flash through SMI
  194. * @dev: structure of SMI information.
  195. * @bank: bank to which flash is connected
  196. *
  197. * This routine will return the status register of the flash chip present at the
  198. * given bank.
  199. */
  200. static int spear_smi_read_sr(struct spear_smi *dev, u32 bank)
  201. {
  202. int ret;
  203. u32 ctrlreg1;
  204. mutex_lock(&dev->lock);
  205. dev->status = 0; /* Will be set in interrupt handler */
  206. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  207. /* program smi in hw mode */
  208. writel(ctrlreg1 & ~(SW_MODE | WB_MODE), dev->io_base + SMI_CR1);
  209. /* performing a rsr instruction in hw mode */
  210. writel((bank << BANK_SHIFT) | RD_STATUS_REG | TFIE,
  211. dev->io_base + SMI_CR2);
  212. /* wait for tff */
  213. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  214. dev->status & TFF, SMI_CMD_TIMEOUT);
  215. /* copy dev->status (lower 16 bits) in order to release lock */
  216. if (ret > 0)
  217. ret = dev->status & 0xffff;
  218. else
  219. ret = -EIO;
  220. /* restore the ctrl regs state */
  221. writel(ctrlreg1, dev->io_base + SMI_CR1);
  222. writel(0, dev->io_base + SMI_CR2);
  223. mutex_unlock(&dev->lock);
  224. return ret;
  225. }
  226. /**
  227. * spear_smi_wait_till_ready - wait till flash is ready
  228. * @dev: structure of SMI information.
  229. * @bank: flash corresponding to this bank
  230. * @timeout: timeout for busy wait condition
  231. *
  232. * This routine checks for WIP (write in progress) bit in Status register
  233. * If successful the routine returns 0 else -EBUSY
  234. */
  235. static int spear_smi_wait_till_ready(struct spear_smi *dev, u32 bank,
  236. unsigned long timeout)
  237. {
  238. unsigned long finish;
  239. int status;
  240. finish = jiffies + timeout;
  241. do {
  242. status = spear_smi_read_sr(dev, bank);
  243. if (status < 0)
  244. continue; /* try till timeout */
  245. else if (!(status & SR_WIP))
  246. return 0;
  247. cond_resched();
  248. } while (!time_after_eq(jiffies, finish));
  249. dev_err(&dev->pdev->dev, "smi controller is busy, timeout\n");
  250. return status;
  251. }
  252. /**
  253. * spear_smi_int_handler - SMI Interrupt Handler.
  254. * @irq: irq number
  255. * @dev_id: structure of SMI device, embedded in dev_id.
  256. *
  257. * The handler clears all interrupt conditions and records the status in
  258. * dev->status which is used by the driver later.
  259. */
  260. static irqreturn_t spear_smi_int_handler(int irq, void *dev_id)
  261. {
  262. u32 status = 0;
  263. struct spear_smi *dev = dev_id;
  264. status = readl(dev->io_base + SMI_SR);
  265. if (unlikely(!status))
  266. return IRQ_NONE;
  267. /* clear all interrupt conditions */
  268. writel(0, dev->io_base + SMI_SR);
  269. /* copy the status register in dev->status */
  270. dev->status |= status;
  271. /* send the completion */
  272. wake_up_interruptible(&dev->cmd_complete);
  273. return IRQ_HANDLED;
  274. }
  275. /**
  276. * spear_smi_hw_init - initializes the smi controller.
  277. * @dev: structure of smi device
  278. *
  279. * this routine initializes the smi controller wit the default values
  280. */
  281. static void spear_smi_hw_init(struct spear_smi *dev)
  282. {
  283. unsigned long rate = 0;
  284. u32 prescale = 0;
  285. u32 val;
  286. rate = clk_get_rate(dev->clk);
  287. /* functional clock of smi */
  288. prescale = DIV_ROUND_UP(rate, dev->clk_rate);
  289. /*
  290. * setting the standard values, fast mode, prescaler for
  291. * SMI_MAX_CLOCK_FREQ (50MHz) operation and bank enable
  292. */
  293. val = HOLD1 | BANK_EN | DSEL_TIME | (prescale << 8);
  294. mutex_lock(&dev->lock);
  295. /* clear all interrupt conditions */
  296. writel(0, dev->io_base + SMI_SR);
  297. writel(val, dev->io_base + SMI_CR1);
  298. mutex_unlock(&dev->lock);
  299. }
  300. /**
  301. * get_flash_index - match chip id from a flash list.
  302. * @flash_id: a valid nor flash chip id obtained from board.
  303. *
  304. * try to validate the chip id by matching from a list, if not found then simply
  305. * returns negative. In case of success returns index in to the flash devices
  306. * array.
  307. */
  308. static int get_flash_index(u32 flash_id)
  309. {
  310. int index;
  311. /* Matches chip-id to entire list of 'serial-nor flash' ids */
  312. for (index = 0; index < ARRAY_SIZE(flash_devices); index++) {
  313. if (flash_devices[index].device_id == flash_id)
  314. return index;
  315. }
  316. /* Memory chip is not listed and not supported */
  317. return -ENODEV;
  318. }
  319. /**
  320. * spear_smi_write_enable - Enable the flash to do write operation
  321. * @dev: structure of SMI device
  322. * @bank: enable write for flash connected to this bank
  323. *
  324. * Set write enable latch with Write Enable command.
  325. * Returns 0 on success.
  326. */
  327. static int spear_smi_write_enable(struct spear_smi *dev, u32 bank)
  328. {
  329. int ret;
  330. u32 ctrlreg1;
  331. mutex_lock(&dev->lock);
  332. dev->status = 0; /* Will be set in interrupt handler */
  333. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  334. /* program smi in h/w mode */
  335. writel(ctrlreg1 & ~SW_MODE, dev->io_base + SMI_CR1);
  336. /* give the flash, write enable command */
  337. writel((bank << BANK_SHIFT) | WE | TFIE, dev->io_base + SMI_CR2);
  338. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  339. dev->status & TFF, SMI_CMD_TIMEOUT);
  340. /* restore the ctrl regs state */
  341. writel(ctrlreg1, dev->io_base + SMI_CR1);
  342. writel(0, dev->io_base + SMI_CR2);
  343. if (ret <= 0) {
  344. ret = -EIO;
  345. dev_err(&dev->pdev->dev,
  346. "smi controller failed on write enable\n");
  347. } else {
  348. /* check whether write mode status is set for required bank */
  349. if (dev->status & (1 << (bank + WM_SHIFT)))
  350. ret = 0;
  351. else {
  352. dev_err(&dev->pdev->dev, "couldn't enable write\n");
  353. ret = -EIO;
  354. }
  355. }
  356. mutex_unlock(&dev->lock);
  357. return ret;
  358. }
  359. static inline u32
  360. get_sector_erase_cmd(struct spear_snor_flash *flash, u32 offset)
  361. {
  362. u32 cmd;
  363. u8 *x = (u8 *)&cmd;
  364. x[0] = flash->erase_cmd;
  365. x[1] = offset >> 16;
  366. x[2] = offset >> 8;
  367. x[3] = offset;
  368. return cmd;
  369. }
  370. /**
  371. * spear_smi_erase_sector - erase one sector of flash
  372. * @dev: structure of SMI information
  373. * @command: erase command to be send
  374. * @bank: bank to which this command needs to be send
  375. * @bytes: size of command
  376. *
  377. * Erase one sector of flash memory at offset ``offset'' which is any
  378. * address within the sector which should be erased.
  379. * Returns 0 if successful, non-zero otherwise.
  380. */
  381. static int spear_smi_erase_sector(struct spear_smi *dev,
  382. u32 bank, u32 command, u32 bytes)
  383. {
  384. u32 ctrlreg1 = 0;
  385. int ret;
  386. ret = spear_smi_wait_till_ready(dev, bank, SMI_MAX_TIME_OUT);
  387. if (ret)
  388. return ret;
  389. ret = spear_smi_write_enable(dev, bank);
  390. if (ret)
  391. return ret;
  392. mutex_lock(&dev->lock);
  393. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  394. writel((ctrlreg1 | SW_MODE) & ~WB_MODE, dev->io_base + SMI_CR1);
  395. /* send command in sw mode */
  396. writel(command, dev->io_base + SMI_TR);
  397. writel((bank << BANK_SHIFT) | SEND | TFIE | (bytes << TX_LEN_SHIFT),
  398. dev->io_base + SMI_CR2);
  399. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  400. dev->status & TFF, SMI_CMD_TIMEOUT);
  401. if (ret <= 0) {
  402. ret = -EIO;
  403. dev_err(&dev->pdev->dev, "sector erase failed\n");
  404. } else
  405. ret = 0; /* success */
  406. /* restore ctrl regs */
  407. writel(ctrlreg1, dev->io_base + SMI_CR1);
  408. writel(0, dev->io_base + SMI_CR2);
  409. mutex_unlock(&dev->lock);
  410. return ret;
  411. }
  412. /**
  413. * spear_mtd_erase - perform flash erase operation as requested by user
  414. * @mtd: Provides the memory characteristics
  415. * @e_info: Provides the erase information
  416. *
  417. * Erase an address range on the flash chip. The address range may extend
  418. * one or more erase sectors. Return an error is there is a problem erasing.
  419. */
  420. static int spear_mtd_erase(struct mtd_info *mtd, struct erase_info *e_info)
  421. {
  422. struct spear_snor_flash *flash = get_flash_data(mtd);
  423. struct spear_smi *dev = mtd->priv;
  424. u32 addr, command, bank;
  425. int len, ret;
  426. if (!flash || !dev)
  427. return -ENODEV;
  428. bank = flash->bank;
  429. if (bank > dev->num_flashes - 1) {
  430. dev_err(&dev->pdev->dev, "Invalid Bank Num");
  431. return -EINVAL;
  432. }
  433. addr = e_info->addr;
  434. len = e_info->len;
  435. mutex_lock(&flash->lock);
  436. /* now erase sectors in loop */
  437. while (len) {
  438. command = get_sector_erase_cmd(flash, addr);
  439. /* preparing the command for flash */
  440. ret = spear_smi_erase_sector(dev, bank, command, 4);
  441. if (ret) {
  442. e_info->state = MTD_ERASE_FAILED;
  443. mutex_unlock(&flash->lock);
  444. return ret;
  445. }
  446. addr += mtd->erasesize;
  447. len -= mtd->erasesize;
  448. }
  449. mutex_unlock(&flash->lock);
  450. e_info->state = MTD_ERASE_DONE;
  451. mtd_erase_callback(e_info);
  452. return 0;
  453. }
  454. /**
  455. * spear_mtd_read - performs flash read operation as requested by the user
  456. * @mtd: MTD information of the memory bank
  457. * @from: Address from which to start read
  458. * @len: Number of bytes to be read
  459. * @retlen: Fills the Number of bytes actually read
  460. * @buf: Fills this after reading
  461. *
  462. * Read an address range from the flash chip. The address range
  463. * may be any size provided it is within the physical boundaries.
  464. * Returns 0 on success, non zero otherwise
  465. */
  466. static int spear_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  467. size_t *retlen, u8 *buf)
  468. {
  469. struct spear_snor_flash *flash = get_flash_data(mtd);
  470. struct spear_smi *dev = mtd->priv;
  471. void *src;
  472. u32 ctrlreg1, val;
  473. int ret;
  474. if (!flash || !dev)
  475. return -ENODEV;
  476. if (flash->bank > dev->num_flashes - 1) {
  477. dev_err(&dev->pdev->dev, "Invalid Bank Num");
  478. return -EINVAL;
  479. }
  480. /* select address as per bank number */
  481. src = flash->base_addr + from;
  482. mutex_lock(&flash->lock);
  483. /* wait till previous write/erase is done. */
  484. ret = spear_smi_wait_till_ready(dev, flash->bank, SMI_MAX_TIME_OUT);
  485. if (ret) {
  486. mutex_unlock(&flash->lock);
  487. return ret;
  488. }
  489. mutex_lock(&dev->lock);
  490. /* put smi in hw mode not wbt mode */
  491. ctrlreg1 = val = readl(dev->io_base + SMI_CR1);
  492. val &= ~(SW_MODE | WB_MODE);
  493. if (flash->fast_mode)
  494. val |= FAST_MODE;
  495. writel(val, dev->io_base + SMI_CR1);
  496. memcpy_fromio(buf, (u8 *)src, len);
  497. /* restore ctrl reg1 */
  498. writel(ctrlreg1, dev->io_base + SMI_CR1);
  499. mutex_unlock(&dev->lock);
  500. *retlen = len;
  501. mutex_unlock(&flash->lock);
  502. return 0;
  503. }
  504. static inline int spear_smi_cpy_toio(struct spear_smi *dev, u32 bank,
  505. void *dest, const void *src, size_t len)
  506. {
  507. int ret;
  508. u32 ctrlreg1;
  509. /* wait until finished previous write command. */
  510. ret = spear_smi_wait_till_ready(dev, bank, SMI_MAX_TIME_OUT);
  511. if (ret)
  512. return ret;
  513. /* put smi in write enable */
  514. ret = spear_smi_write_enable(dev, bank);
  515. if (ret)
  516. return ret;
  517. /* put smi in hw, write burst mode */
  518. mutex_lock(&dev->lock);
  519. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  520. writel((ctrlreg1 | WB_MODE) & ~SW_MODE, dev->io_base + SMI_CR1);
  521. memcpy_toio(dest, src, len);
  522. writel(ctrlreg1, dev->io_base + SMI_CR1);
  523. mutex_unlock(&dev->lock);
  524. return 0;
  525. }
  526. /**
  527. * spear_mtd_write - performs write operation as requested by the user.
  528. * @mtd: MTD information of the memory bank.
  529. * @to: Address to write.
  530. * @len: Number of bytes to be written.
  531. * @retlen: Number of bytes actually wrote.
  532. * @buf: Buffer from which the data to be taken.
  533. *
  534. * Write an address range to the flash chip. Data must be written in
  535. * flash_page_size chunks. The address range may be any size provided
  536. * it is within the physical boundaries.
  537. * Returns 0 on success, non zero otherwise
  538. */
  539. static int spear_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  540. size_t *retlen, const u8 *buf)
  541. {
  542. struct spear_snor_flash *flash = get_flash_data(mtd);
  543. struct spear_smi *dev = mtd->priv;
  544. void *dest;
  545. u32 page_offset, page_size;
  546. int ret;
  547. if (!flash || !dev)
  548. return -ENODEV;
  549. if (flash->bank > dev->num_flashes - 1) {
  550. dev_err(&dev->pdev->dev, "Invalid Bank Num");
  551. return -EINVAL;
  552. }
  553. /* select address as per bank number */
  554. dest = flash->base_addr + to;
  555. mutex_lock(&flash->lock);
  556. page_offset = (u32)to % flash->page_size;
  557. /* do if all the bytes fit onto one page */
  558. if (page_offset + len <= flash->page_size) {
  559. ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf, len);
  560. if (!ret)
  561. *retlen += len;
  562. } else {
  563. u32 i;
  564. /* the size of data remaining on the first page */
  565. page_size = flash->page_size - page_offset;
  566. ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf,
  567. page_size);
  568. if (ret)
  569. goto err_write;
  570. else
  571. *retlen += page_size;
  572. /* write everything in pagesize chunks */
  573. for (i = page_size; i < len; i += page_size) {
  574. page_size = len - i;
  575. if (page_size > flash->page_size)
  576. page_size = flash->page_size;
  577. ret = spear_smi_cpy_toio(dev, flash->bank, dest + i,
  578. buf + i, page_size);
  579. if (ret)
  580. break;
  581. else
  582. *retlen += page_size;
  583. }
  584. }
  585. err_write:
  586. mutex_unlock(&flash->lock);
  587. return ret;
  588. }
  589. /**
  590. * spear_smi_probe_flash - Detects the NOR Flash chip.
  591. * @dev: structure of SMI information.
  592. * @bank: bank on which flash must be probed
  593. *
  594. * This routine will check whether there exists a flash chip on a given memory
  595. * bank ID.
  596. * Return index of the probed flash in flash devices structure
  597. */
  598. static int spear_smi_probe_flash(struct spear_smi *dev, u32 bank)
  599. {
  600. int ret;
  601. u32 val = 0;
  602. ret = spear_smi_wait_till_ready(dev, bank, SMI_PROBE_TIMEOUT);
  603. if (ret)
  604. return ret;
  605. mutex_lock(&dev->lock);
  606. dev->status = 0; /* Will be set in interrupt handler */
  607. /* put smi in sw mode */
  608. val = readl(dev->io_base + SMI_CR1);
  609. writel(val | SW_MODE, dev->io_base + SMI_CR1);
  610. /* send readid command in sw mode */
  611. writel(OPCODE_RDID, dev->io_base + SMI_TR);
  612. val = (bank << BANK_SHIFT) | SEND | (1 << TX_LEN_SHIFT) |
  613. (3 << RX_LEN_SHIFT) | TFIE;
  614. writel(val, dev->io_base + SMI_CR2);
  615. /* wait for TFF */
  616. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  617. dev->status & TFF, SMI_CMD_TIMEOUT);
  618. if (ret <= 0) {
  619. ret = -ENODEV;
  620. goto err_probe;
  621. }
  622. /* get memory chip id */
  623. val = readl(dev->io_base + SMI_RR);
  624. val &= 0x00ffffff;
  625. ret = get_flash_index(val);
  626. err_probe:
  627. /* clear sw mode */
  628. val = readl(dev->io_base + SMI_CR1);
  629. writel(val & ~SW_MODE, dev->io_base + SMI_CR1);
  630. mutex_unlock(&dev->lock);
  631. return ret;
  632. }
  633. #ifdef CONFIG_OF
  634. static int __devinit spear_smi_probe_config_dt(struct platform_device *pdev,
  635. struct device_node *np)
  636. {
  637. struct spear_smi_plat_data *pdata = dev_get_platdata(&pdev->dev);
  638. struct device_node *pp = NULL;
  639. const __be32 *addr;
  640. u32 val;
  641. int len;
  642. int i = 0;
  643. if (!np)
  644. return -ENODEV;
  645. of_property_read_u32(np, "clock-rate", &val);
  646. pdata->clk_rate = val;
  647. pdata->board_flash_info = devm_kzalloc(&pdev->dev,
  648. sizeof(*pdata->board_flash_info),
  649. GFP_KERNEL);
  650. /* Fill structs for each subnode (flash device) */
  651. while ((pp = of_get_next_child(np, pp))) {
  652. struct spear_smi_flash_info *flash_info;
  653. flash_info = &pdata->board_flash_info[i];
  654. pdata->np[i] = pp;
  655. /* Read base-addr and size from DT */
  656. addr = of_get_property(pp, "reg", &len);
  657. pdata->board_flash_info->mem_base = be32_to_cpup(&addr[0]);
  658. pdata->board_flash_info->size = be32_to_cpup(&addr[1]);
  659. if (of_get_property(pp, "st,smi-fast-mode", NULL))
  660. pdata->board_flash_info->fast_mode = 1;
  661. i++;
  662. }
  663. pdata->num_flashes = i;
  664. return 0;
  665. }
  666. #else
  667. static int __devinit spear_smi_probe_config_dt(struct platform_device *pdev,
  668. struct device_node *np)
  669. {
  670. return -ENOSYS;
  671. }
  672. #endif
  673. static int spear_smi_setup_banks(struct platform_device *pdev,
  674. u32 bank, struct device_node *np)
  675. {
  676. struct spear_smi *dev = platform_get_drvdata(pdev);
  677. struct mtd_part_parser_data ppdata = {};
  678. struct spear_smi_flash_info *flash_info;
  679. struct spear_smi_plat_data *pdata;
  680. struct spear_snor_flash *flash;
  681. struct mtd_partition *parts = NULL;
  682. int count = 0;
  683. int flash_index;
  684. int ret = 0;
  685. pdata = dev_get_platdata(&pdev->dev);
  686. if (bank > pdata->num_flashes - 1)
  687. return -EINVAL;
  688. flash_info = &pdata->board_flash_info[bank];
  689. if (!flash_info)
  690. return -ENODEV;
  691. flash = kzalloc(sizeof(*flash), GFP_ATOMIC);
  692. if (!flash)
  693. return -ENOMEM;
  694. flash->bank = bank;
  695. flash->fast_mode = flash_info->fast_mode ? 1 : 0;
  696. mutex_init(&flash->lock);
  697. /* verify whether nor flash is really present on board */
  698. flash_index = spear_smi_probe_flash(dev, bank);
  699. if (flash_index < 0) {
  700. dev_info(&dev->pdev->dev, "smi-nor%d not found\n", bank);
  701. ret = flash_index;
  702. goto err_probe;
  703. }
  704. /* map the memory for nor flash chip */
  705. flash->base_addr = ioremap(flash_info->mem_base, flash_info->size);
  706. if (!flash->base_addr) {
  707. ret = -EIO;
  708. goto err_probe;
  709. }
  710. dev->flash[bank] = flash;
  711. flash->mtd.priv = dev;
  712. if (flash_info->name)
  713. flash->mtd.name = flash_info->name;
  714. else
  715. flash->mtd.name = flash_devices[flash_index].name;
  716. flash->mtd.type = MTD_NORFLASH;
  717. flash->mtd.writesize = 1;
  718. flash->mtd.flags = MTD_CAP_NORFLASH;
  719. flash->mtd.size = flash_info->size;
  720. flash->mtd.erasesize = flash_devices[flash_index].sectorsize;
  721. flash->page_size = flash_devices[flash_index].pagesize;
  722. flash->mtd.writebufsize = flash->page_size;
  723. flash->erase_cmd = flash_devices[flash_index].erase_cmd;
  724. flash->mtd._erase = spear_mtd_erase;
  725. flash->mtd._read = spear_mtd_read;
  726. flash->mtd._write = spear_mtd_write;
  727. flash->dev_id = flash_devices[flash_index].device_id;
  728. dev_info(&dev->pdev->dev, "mtd .name=%s .size=%llx(%lluM)\n",
  729. flash->mtd.name, flash->mtd.size,
  730. flash->mtd.size / (1024 * 1024));
  731. dev_info(&dev->pdev->dev, ".erasesize = 0x%x(%uK)\n",
  732. flash->mtd.erasesize, flash->mtd.erasesize / 1024);
  733. #ifndef CONFIG_OF
  734. if (flash_info->partitions) {
  735. parts = flash_info->partitions;
  736. count = flash_info->nr_partitions;
  737. }
  738. #endif
  739. ppdata.of_node = np;
  740. ret = mtd_device_parse_register(&flash->mtd, NULL, &ppdata, parts,
  741. count);
  742. if (ret) {
  743. dev_err(&dev->pdev->dev, "Err MTD partition=%d\n", ret);
  744. goto err_map;
  745. }
  746. return 0;
  747. err_map:
  748. iounmap(flash->base_addr);
  749. err_probe:
  750. kfree(flash);
  751. return ret;
  752. }
  753. /**
  754. * spear_smi_probe - Entry routine
  755. * @pdev: platform device structure
  756. *
  757. * This is the first routine which gets invoked during booting and does all
  758. * initialization/allocation work. The routine looks for available memory banks,
  759. * and do proper init for any found one.
  760. * Returns 0 on success, non zero otherwise
  761. */
  762. static int __devinit spear_smi_probe(struct platform_device *pdev)
  763. {
  764. struct device_node *np = pdev->dev.of_node;
  765. struct spear_smi_plat_data *pdata = NULL;
  766. struct spear_smi *dev;
  767. struct resource *smi_base;
  768. int irq, ret = 0;
  769. int i;
  770. if (np) {
  771. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  772. if (!pdata) {
  773. pr_err("%s: ERROR: no memory", __func__);
  774. ret = -ENOMEM;
  775. goto err;
  776. }
  777. pdev->dev.platform_data = pdata;
  778. ret = spear_smi_probe_config_dt(pdev, np);
  779. if (ret) {
  780. ret = -ENODEV;
  781. dev_err(&pdev->dev, "no platform data\n");
  782. goto err;
  783. }
  784. } else {
  785. pdata = dev_get_platdata(&pdev->dev);
  786. if (pdata < 0) {
  787. ret = -ENODEV;
  788. dev_err(&pdev->dev, "no platform data\n");
  789. goto err;
  790. }
  791. }
  792. smi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  793. if (!smi_base) {
  794. ret = -ENODEV;
  795. dev_err(&pdev->dev, "invalid smi base address\n");
  796. goto err;
  797. }
  798. irq = platform_get_irq(pdev, 0);
  799. if (irq < 0) {
  800. ret = -ENODEV;
  801. dev_err(&pdev->dev, "invalid smi irq\n");
  802. goto err;
  803. }
  804. dev = kzalloc(sizeof(*dev), GFP_ATOMIC);
  805. if (!dev) {
  806. ret = -ENOMEM;
  807. dev_err(&pdev->dev, "mem alloc fail\n");
  808. goto err;
  809. }
  810. smi_base = request_mem_region(smi_base->start, resource_size(smi_base),
  811. pdev->name);
  812. if (!smi_base) {
  813. ret = -EBUSY;
  814. dev_err(&pdev->dev, "request mem region fail\n");
  815. goto err_mem;
  816. }
  817. dev->io_base = ioremap(smi_base->start, resource_size(smi_base));
  818. if (!dev->io_base) {
  819. ret = -EIO;
  820. dev_err(&pdev->dev, "ioremap fail\n");
  821. goto err_ioremap;
  822. }
  823. dev->pdev = pdev;
  824. dev->clk_rate = pdata->clk_rate;
  825. if (dev->clk_rate < 0 || dev->clk_rate > SMI_MAX_CLOCK_FREQ)
  826. dev->clk_rate = SMI_MAX_CLOCK_FREQ;
  827. dev->num_flashes = pdata->num_flashes;
  828. if (dev->num_flashes > MAX_NUM_FLASH_CHIP) {
  829. dev_err(&pdev->dev, "exceeding max number of flashes\n");
  830. dev->num_flashes = MAX_NUM_FLASH_CHIP;
  831. }
  832. dev->clk = clk_get(&pdev->dev, NULL);
  833. if (IS_ERR(dev->clk)) {
  834. ret = PTR_ERR(dev->clk);
  835. goto err_clk;
  836. }
  837. ret = clk_prepare_enable(dev->clk);
  838. if (ret)
  839. goto err_clk_prepare_enable;
  840. ret = request_irq(irq, spear_smi_int_handler, 0, pdev->name, dev);
  841. if (ret) {
  842. dev_err(&dev->pdev->dev, "SMI IRQ allocation failed\n");
  843. goto err_irq;
  844. }
  845. mutex_init(&dev->lock);
  846. init_waitqueue_head(&dev->cmd_complete);
  847. spear_smi_hw_init(dev);
  848. platform_set_drvdata(pdev, dev);
  849. /* loop for each serial nor-flash which is connected to smi */
  850. for (i = 0; i < dev->num_flashes; i++) {
  851. ret = spear_smi_setup_banks(pdev, i, pdata->np[i]);
  852. if (ret) {
  853. dev_err(&dev->pdev->dev, "bank setup failed\n");
  854. goto err_bank_setup;
  855. }
  856. }
  857. return 0;
  858. err_bank_setup:
  859. free_irq(irq, dev);
  860. platform_set_drvdata(pdev, NULL);
  861. err_irq:
  862. clk_disable_unprepare(dev->clk);
  863. err_clk_prepare_enable:
  864. clk_put(dev->clk);
  865. err_clk:
  866. iounmap(dev->io_base);
  867. err_ioremap:
  868. release_mem_region(smi_base->start, resource_size(smi_base));
  869. err_mem:
  870. kfree(dev);
  871. err:
  872. return ret;
  873. }
  874. /**
  875. * spear_smi_remove - Exit routine
  876. * @pdev: platform device structure
  877. *
  878. * free all allocations and delete the partitions.
  879. */
  880. static int __devexit spear_smi_remove(struct platform_device *pdev)
  881. {
  882. struct spear_smi *dev;
  883. struct spear_smi_plat_data *pdata;
  884. struct spear_snor_flash *flash;
  885. struct resource *smi_base;
  886. int ret;
  887. int i, irq;
  888. dev = platform_get_drvdata(pdev);
  889. if (!dev) {
  890. dev_err(&pdev->dev, "dev is null\n");
  891. return -ENODEV;
  892. }
  893. pdata = dev_get_platdata(&pdev->dev);
  894. /* clean up for all nor flash */
  895. for (i = 0; i < dev->num_flashes; i++) {
  896. flash = dev->flash[i];
  897. if (!flash)
  898. continue;
  899. /* clean up mtd stuff */
  900. ret = mtd_device_unregister(&flash->mtd);
  901. if (ret)
  902. dev_err(&pdev->dev, "error removing mtd\n");
  903. iounmap(flash->base_addr);
  904. kfree(flash);
  905. }
  906. irq = platform_get_irq(pdev, 0);
  907. free_irq(irq, dev);
  908. clk_disable_unprepare(dev->clk);
  909. clk_put(dev->clk);
  910. iounmap(dev->io_base);
  911. kfree(dev);
  912. smi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  913. release_mem_region(smi_base->start, resource_size(smi_base));
  914. platform_set_drvdata(pdev, NULL);
  915. return 0;
  916. }
  917. #ifdef CONFIG_PM
  918. static int spear_smi_suspend(struct device *dev)
  919. {
  920. struct spear_smi *sdev = dev_get_drvdata(dev);
  921. if (sdev && sdev->clk)
  922. clk_disable_unprepare(sdev->clk);
  923. return 0;
  924. }
  925. static int spear_smi_resume(struct device *dev)
  926. {
  927. struct spear_smi *sdev = dev_get_drvdata(dev);
  928. int ret = -EPERM;
  929. if (sdev && sdev->clk)
  930. ret = clk_prepare_enable(sdev->clk);
  931. if (!ret)
  932. spear_smi_hw_init(sdev);
  933. return ret;
  934. }
  935. static SIMPLE_DEV_PM_OPS(spear_smi_pm_ops, spear_smi_suspend, spear_smi_resume);
  936. #endif
  937. #ifdef CONFIG_OF
  938. static const struct of_device_id spear_smi_id_table[] = {
  939. { .compatible = "st,spear600-smi" },
  940. {}
  941. };
  942. MODULE_DEVICE_TABLE(of, spear_smi_id_table);
  943. #endif
  944. static struct platform_driver spear_smi_driver = {
  945. .driver = {
  946. .name = "smi",
  947. .bus = &platform_bus_type,
  948. .owner = THIS_MODULE,
  949. .of_match_table = of_match_ptr(spear_smi_id_table),
  950. #ifdef CONFIG_PM
  951. .pm = &spear_smi_pm_ops,
  952. #endif
  953. },
  954. .probe = spear_smi_probe,
  955. .remove = __devexit_p(spear_smi_remove),
  956. };
  957. static int spear_smi_init(void)
  958. {
  959. return platform_driver_register(&spear_smi_driver);
  960. }
  961. module_init(spear_smi_init);
  962. static void spear_smi_exit(void)
  963. {
  964. platform_driver_unregister(&spear_smi_driver);
  965. }
  966. module_exit(spear_smi_exit);
  967. MODULE_LICENSE("GPL");
  968. MODULE_AUTHOR("Ashish Priyadarshi, Shiraz Hashim <shiraz.hashim@st.com>");
  969. MODULE_DESCRIPTION("MTD SMI driver for serial nor flash chips");