spear_smi.c 30 KB

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