Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Driver for Broadcom BRCMSTB, NSP, NS2, Cygnus SPI Controllers |
| 3 | * |
| 4 | * Copyright 2016 Broadcom |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License, version 2, as |
| 8 | * published by the Free Software Foundation (the "GPL"). |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License version 2 (GPLv2) for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * version 2 (GPLv2) along with this source code. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/clk.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/device.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/io.h> |
| 25 | #include <linux/ioport.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/mtd/cfi.h> |
| 29 | #include <linux/mtd/spi-nor.h> |
| 30 | #include <linux/of.h> |
| 31 | #include <linux/of_irq.h> |
| 32 | #include <linux/platform_device.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/spi/spi.h> |
| 35 | #include <linux/sysfs.h> |
| 36 | #include <linux/types.h> |
| 37 | #include "spi-bcm-qspi.h" |
| 38 | |
| 39 | #define DRIVER_NAME "bcm_qspi" |
| 40 | |
| 41 | /* MSPI register offsets */ |
| 42 | #define MSPI_SPCR0_LSB 0x000 |
| 43 | #define MSPI_SPCR0_MSB 0x004 |
| 44 | #define MSPI_SPCR1_LSB 0x008 |
| 45 | #define MSPI_SPCR1_MSB 0x00c |
| 46 | #define MSPI_NEWQP 0x010 |
| 47 | #define MSPI_ENDQP 0x014 |
| 48 | #define MSPI_SPCR2 0x018 |
| 49 | #define MSPI_MSPI_STATUS 0x020 |
| 50 | #define MSPI_CPTQP 0x024 |
| 51 | #define MSPI_SPCR3 0x028 |
| 52 | #define MSPI_TXRAM 0x040 |
| 53 | #define MSPI_RXRAM 0x0c0 |
| 54 | #define MSPI_CDRAM 0x140 |
| 55 | #define MSPI_WRITE_LOCK 0x180 |
| 56 | |
| 57 | #define MSPI_MASTER_BIT BIT(7) |
| 58 | |
| 59 | #define MSPI_NUM_CDRAM 16 |
| 60 | #define MSPI_CDRAM_CONT_BIT BIT(7) |
| 61 | #define MSPI_CDRAM_BITSE_BIT BIT(6) |
| 62 | #define MSPI_CDRAM_PCS 0xf |
| 63 | |
| 64 | #define MSPI_SPCR2_SPE BIT(6) |
| 65 | #define MSPI_SPCR2_CONT_AFTER_CMD BIT(7) |
| 66 | |
| 67 | #define MSPI_MSPI_STATUS_SPIF BIT(0) |
| 68 | |
| 69 | #define INTR_BASE_BIT_SHIFT 0x02 |
| 70 | #define INTR_COUNT 0x07 |
| 71 | |
| 72 | #define NUM_CHIPSELECT 4 |
| 73 | #define QSPI_SPBR_MIN 8U |
| 74 | #define QSPI_SPBR_MAX 255U |
| 75 | |
| 76 | #define OPCODE_DIOR 0xBB |
| 77 | #define OPCODE_QIOR 0xEB |
| 78 | #define OPCODE_DIOR_4B 0xBC |
| 79 | #define OPCODE_QIOR_4B 0xEC |
| 80 | |
| 81 | #define MAX_CMD_SIZE 6 |
| 82 | |
| 83 | #define ADDR_4MB_MASK GENMASK(22, 0) |
| 84 | |
| 85 | /* stop at end of transfer, no other reason */ |
| 86 | #define TRANS_STATUS_BREAK_NONE 0 |
| 87 | /* stop at end of spi_message */ |
| 88 | #define TRANS_STATUS_BREAK_EOM 1 |
| 89 | /* stop at end of spi_transfer if delay */ |
| 90 | #define TRANS_STATUS_BREAK_DELAY 2 |
| 91 | /* stop at end of spi_transfer if cs_change */ |
| 92 | #define TRANS_STATUS_BREAK_CS_CHANGE 4 |
| 93 | /* stop if we run out of bytes */ |
| 94 | #define TRANS_STATUS_BREAK_NO_BYTES 8 |
| 95 | |
| 96 | /* events that make us stop filling TX slots */ |
| 97 | #define TRANS_STATUS_BREAK_TX (TRANS_STATUS_BREAK_EOM | \ |
| 98 | TRANS_STATUS_BREAK_DELAY | \ |
| 99 | TRANS_STATUS_BREAK_CS_CHANGE) |
| 100 | |
| 101 | /* events that make us deassert CS */ |
| 102 | #define TRANS_STATUS_BREAK_DESELECT (TRANS_STATUS_BREAK_EOM | \ |
| 103 | TRANS_STATUS_BREAK_CS_CHANGE) |
| 104 | |
| 105 | struct bcm_qspi_parms { |
| 106 | u32 speed_hz; |
| 107 | u8 mode; |
| 108 | u8 bits_per_word; |
| 109 | }; |
| 110 | |
| 111 | enum base_type { |
| 112 | MSPI, |
| 113 | CHIP_SELECT, |
| 114 | BASEMAX, |
| 115 | }; |
| 116 | |
| 117 | struct bcm_qspi_irq { |
| 118 | const char *irq_name; |
| 119 | const irq_handler_t irq_handler; |
| 120 | u32 mask; |
| 121 | }; |
| 122 | |
| 123 | struct bcm_qspi_dev_id { |
| 124 | const struct bcm_qspi_irq *irqp; |
| 125 | void *dev; |
| 126 | }; |
| 127 | |
| 128 | struct qspi_trans { |
| 129 | struct spi_transfer *trans; |
| 130 | int byte; |
| 131 | }; |
| 132 | |
| 133 | struct bcm_qspi { |
| 134 | struct platform_device *pdev; |
| 135 | struct spi_master *master; |
| 136 | struct clk *clk; |
| 137 | u32 base_clk; |
| 138 | u32 max_speed_hz; |
| 139 | void __iomem *base[BASEMAX]; |
| 140 | struct bcm_qspi_parms last_parms; |
| 141 | struct qspi_trans trans_pos; |
| 142 | int curr_cs; |
| 143 | u32 s3_strap_override_ctrl; |
| 144 | bool big_endian; |
| 145 | int num_irqs; |
| 146 | struct bcm_qspi_dev_id *dev_ids; |
| 147 | struct completion mspi_done; |
| 148 | }; |
| 149 | |
| 150 | /* Read qspi controller register*/ |
| 151 | static inline u32 bcm_qspi_read(struct bcm_qspi *qspi, enum base_type type, |
| 152 | unsigned int offset) |
| 153 | { |
| 154 | return bcm_qspi_readl(qspi->big_endian, qspi->base[type] + offset); |
| 155 | } |
| 156 | |
| 157 | /* Write qspi controller register*/ |
| 158 | static inline void bcm_qspi_write(struct bcm_qspi *qspi, enum base_type type, |
| 159 | unsigned int offset, unsigned int data) |
| 160 | { |
| 161 | bcm_qspi_writel(qspi->big_endian, data, qspi->base[type] + offset); |
| 162 | } |
| 163 | |
| 164 | static void bcm_qspi_chip_select(struct bcm_qspi *qspi, int cs) |
| 165 | { |
| 166 | u32 data = 0; |
| 167 | |
| 168 | if (qspi->curr_cs == cs) |
| 169 | return; |
| 170 | if (qspi->base[CHIP_SELECT]) { |
| 171 | data = bcm_qspi_read(qspi, CHIP_SELECT, 0); |
| 172 | data = (data & ~0xff) | (1 << cs); |
| 173 | bcm_qspi_write(qspi, CHIP_SELECT, 0, data); |
| 174 | usleep_range(10, 20); |
| 175 | } |
| 176 | qspi->curr_cs = cs; |
| 177 | } |
| 178 | |
| 179 | /* MSPI helpers */ |
| 180 | static void bcm_qspi_hw_set_parms(struct bcm_qspi *qspi, |
| 181 | const struct bcm_qspi_parms *xp) |
| 182 | { |
| 183 | u32 spcr, spbr = 0; |
| 184 | |
| 185 | if (xp->speed_hz) |
| 186 | spbr = qspi->base_clk / (2 * xp->speed_hz); |
| 187 | |
| 188 | spcr = clamp_val(spbr, QSPI_SPBR_MIN, QSPI_SPBR_MAX); |
| 189 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR0_LSB, spcr); |
| 190 | |
| 191 | spcr = MSPI_MASTER_BIT; |
| 192 | /* for 16 bit the data should be zero */ |
| 193 | if (xp->bits_per_word != 16) |
| 194 | spcr |= xp->bits_per_word << 2; |
| 195 | spcr |= xp->mode & 3; |
| 196 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR0_MSB, spcr); |
| 197 | |
| 198 | qspi->last_parms = *xp; |
| 199 | } |
| 200 | |
| 201 | static void bcm_qspi_update_parms(struct bcm_qspi *qspi, |
| 202 | struct spi_device *spi, |
| 203 | struct spi_transfer *trans) |
| 204 | { |
| 205 | struct bcm_qspi_parms xp; |
| 206 | |
| 207 | xp.speed_hz = trans->speed_hz; |
| 208 | xp.bits_per_word = trans->bits_per_word; |
| 209 | xp.mode = spi->mode; |
| 210 | |
| 211 | bcm_qspi_hw_set_parms(qspi, &xp); |
| 212 | } |
| 213 | |
| 214 | static int bcm_qspi_setup(struct spi_device *spi) |
| 215 | { |
| 216 | struct bcm_qspi_parms *xp; |
| 217 | |
| 218 | if (spi->bits_per_word > 16) |
| 219 | return -EINVAL; |
| 220 | |
| 221 | xp = spi_get_ctldata(spi); |
| 222 | if (!xp) { |
| 223 | xp = kzalloc(sizeof(*xp), GFP_KERNEL); |
| 224 | if (!xp) |
| 225 | return -ENOMEM; |
| 226 | spi_set_ctldata(spi, xp); |
| 227 | } |
| 228 | xp->speed_hz = spi->max_speed_hz; |
| 229 | xp->mode = spi->mode; |
| 230 | |
| 231 | if (spi->bits_per_word) |
| 232 | xp->bits_per_word = spi->bits_per_word; |
| 233 | else |
| 234 | xp->bits_per_word = 8; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int update_qspi_trans_byte_count(struct bcm_qspi *qspi, |
| 240 | struct qspi_trans *qt, int flags) |
| 241 | { |
| 242 | int ret = TRANS_STATUS_BREAK_NONE; |
| 243 | |
| 244 | /* count the last transferred bytes */ |
| 245 | if (qt->trans->bits_per_word <= 8) |
| 246 | qt->byte++; |
| 247 | else |
| 248 | qt->byte += 2; |
| 249 | |
| 250 | if (qt->byte >= qt->trans->len) { |
| 251 | /* we're at the end of the spi_transfer */ |
| 252 | |
| 253 | /* in TX mode, need to pause for a delay or CS change */ |
| 254 | if (qt->trans->delay_usecs && |
| 255 | (flags & TRANS_STATUS_BREAK_DELAY)) |
| 256 | ret |= TRANS_STATUS_BREAK_DELAY; |
| 257 | if (qt->trans->cs_change && |
| 258 | (flags & TRANS_STATUS_BREAK_CS_CHANGE)) |
| 259 | ret |= TRANS_STATUS_BREAK_CS_CHANGE; |
| 260 | if (ret) |
| 261 | goto done; |
| 262 | |
| 263 | dev_dbg(&qspi->pdev->dev, "advance msg exit\n"); |
| 264 | if (spi_transfer_is_last(qspi->master, qt->trans)) |
| 265 | ret = TRANS_STATUS_BREAK_EOM; |
| 266 | else |
| 267 | ret = TRANS_STATUS_BREAK_NO_BYTES; |
| 268 | |
| 269 | qt->trans = NULL; |
| 270 | } |
| 271 | |
| 272 | done: |
| 273 | dev_dbg(&qspi->pdev->dev, "trans %p len %d byte %d ret %x\n", |
| 274 | qt->trans, qt->trans ? qt->trans->len : 0, qt->byte, ret); |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static inline u8 read_rxram_slot_u8(struct bcm_qspi *qspi, int slot) |
| 279 | { |
| 280 | u32 slot_offset = MSPI_RXRAM + (slot << 3) + 0x4; |
| 281 | |
| 282 | /* mask out reserved bits */ |
| 283 | return bcm_qspi_read(qspi, MSPI, slot_offset) & 0xff; |
| 284 | } |
| 285 | |
| 286 | static inline u16 read_rxram_slot_u16(struct bcm_qspi *qspi, int slot) |
| 287 | { |
| 288 | u32 reg_offset = MSPI_RXRAM; |
| 289 | u32 lsb_offset = reg_offset + (slot << 3) + 0x4; |
| 290 | u32 msb_offset = reg_offset + (slot << 3); |
| 291 | |
| 292 | return (bcm_qspi_read(qspi, MSPI, lsb_offset) & 0xff) | |
| 293 | ((bcm_qspi_read(qspi, MSPI, msb_offset) & 0xff) << 8); |
| 294 | } |
| 295 | |
| 296 | static void read_from_hw(struct bcm_qspi *qspi, int slots) |
| 297 | { |
| 298 | struct qspi_trans tp; |
| 299 | int slot; |
| 300 | |
| 301 | if (slots > MSPI_NUM_CDRAM) { |
| 302 | /* should never happen */ |
| 303 | dev_err(&qspi->pdev->dev, "%s: too many slots!\n", __func__); |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | tp = qspi->trans_pos; |
| 308 | |
| 309 | for (slot = 0; slot < slots; slot++) { |
| 310 | if (tp.trans->bits_per_word <= 8) { |
| 311 | u8 *buf = tp.trans->rx_buf; |
| 312 | |
| 313 | if (buf) |
| 314 | buf[tp.byte] = read_rxram_slot_u8(qspi, slot); |
| 315 | dev_dbg(&qspi->pdev->dev, "RD %02x\n", |
| 316 | buf ? buf[tp.byte] : 0xff); |
| 317 | } else { |
| 318 | u16 *buf = tp.trans->rx_buf; |
| 319 | |
| 320 | if (buf) |
| 321 | buf[tp.byte / 2] = read_rxram_slot_u16(qspi, |
| 322 | slot); |
| 323 | dev_dbg(&qspi->pdev->dev, "RD %04x\n", |
| 324 | buf ? buf[tp.byte] : 0xffff); |
| 325 | } |
| 326 | |
| 327 | update_qspi_trans_byte_count(qspi, &tp, |
| 328 | TRANS_STATUS_BREAK_NONE); |
| 329 | } |
| 330 | |
| 331 | qspi->trans_pos = tp; |
| 332 | } |
| 333 | |
| 334 | static inline void write_txram_slot_u8(struct bcm_qspi *qspi, int slot, |
| 335 | u8 val) |
| 336 | { |
| 337 | u32 reg_offset = MSPI_TXRAM + (slot << 3); |
| 338 | |
| 339 | /* mask out reserved bits */ |
| 340 | bcm_qspi_write(qspi, MSPI, reg_offset, val); |
| 341 | } |
| 342 | |
| 343 | static inline void write_txram_slot_u16(struct bcm_qspi *qspi, int slot, |
| 344 | u16 val) |
| 345 | { |
| 346 | u32 reg_offset = MSPI_TXRAM; |
| 347 | u32 msb_offset = reg_offset + (slot << 3); |
| 348 | u32 lsb_offset = reg_offset + (slot << 3) + 0x4; |
| 349 | |
| 350 | bcm_qspi_write(qspi, MSPI, msb_offset, (val >> 8)); |
| 351 | bcm_qspi_write(qspi, MSPI, lsb_offset, (val & 0xff)); |
| 352 | } |
| 353 | |
| 354 | static inline u32 read_cdram_slot(struct bcm_qspi *qspi, int slot) |
| 355 | { |
| 356 | return bcm_qspi_read(qspi, MSPI, MSPI_CDRAM + (slot << 2)); |
| 357 | } |
| 358 | |
| 359 | static inline void write_cdram_slot(struct bcm_qspi *qspi, int slot, u32 val) |
| 360 | { |
| 361 | bcm_qspi_write(qspi, MSPI, (MSPI_CDRAM + (slot << 2)), val); |
| 362 | } |
| 363 | |
| 364 | /* Return number of slots written */ |
| 365 | static int write_to_hw(struct bcm_qspi *qspi, struct spi_device *spi) |
| 366 | { |
| 367 | struct qspi_trans tp; |
| 368 | int slot = 0, tstatus = 0; |
| 369 | u32 mspi_cdram = 0; |
| 370 | |
| 371 | tp = qspi->trans_pos; |
| 372 | bcm_qspi_update_parms(qspi, spi, tp.trans); |
| 373 | |
| 374 | /* Run until end of transfer or reached the max data */ |
| 375 | while (!tstatus && slot < MSPI_NUM_CDRAM) { |
| 376 | if (tp.trans->bits_per_word <= 8) { |
| 377 | const u8 *buf = tp.trans->tx_buf; |
| 378 | u8 val = buf ? buf[tp.byte] : 0xff; |
| 379 | |
| 380 | write_txram_slot_u8(qspi, slot, val); |
| 381 | dev_dbg(&qspi->pdev->dev, "WR %02x\n", val); |
| 382 | } else { |
| 383 | const u16 *buf = tp.trans->tx_buf; |
| 384 | u16 val = buf ? buf[tp.byte / 2] : 0xffff; |
| 385 | |
| 386 | write_txram_slot_u16(qspi, slot, val); |
| 387 | dev_dbg(&qspi->pdev->dev, "WR %04x\n", val); |
| 388 | } |
| 389 | mspi_cdram = MSPI_CDRAM_CONT_BIT; |
| 390 | mspi_cdram |= (~(1 << spi->chip_select) & |
| 391 | MSPI_CDRAM_PCS); |
| 392 | mspi_cdram |= ((tp.trans->bits_per_word <= 8) ? 0 : |
| 393 | MSPI_CDRAM_BITSE_BIT); |
| 394 | |
| 395 | write_cdram_slot(qspi, slot, mspi_cdram); |
| 396 | |
| 397 | tstatus = update_qspi_trans_byte_count(qspi, &tp, |
| 398 | TRANS_STATUS_BREAK_TX); |
| 399 | slot++; |
| 400 | } |
| 401 | |
| 402 | if (!slot) { |
| 403 | dev_err(&qspi->pdev->dev, "%s: no data to send?", __func__); |
| 404 | goto done; |
| 405 | } |
| 406 | |
| 407 | dev_dbg(&qspi->pdev->dev, "submitting %d slots\n", slot); |
| 408 | bcm_qspi_write(qspi, MSPI, MSPI_NEWQP, 0); |
| 409 | bcm_qspi_write(qspi, MSPI, MSPI_ENDQP, slot - 1); |
| 410 | |
| 411 | if (tstatus & TRANS_STATUS_BREAK_DESELECT) { |
| 412 | mspi_cdram = read_cdram_slot(qspi, slot - 1) & |
| 413 | ~MSPI_CDRAM_CONT_BIT; |
| 414 | write_cdram_slot(qspi, slot - 1, mspi_cdram); |
| 415 | } |
| 416 | |
| 417 | /* Must flush previous writes before starting MSPI operation */ |
| 418 | mb(); |
| 419 | /* Set cont | spe | spifie */ |
| 420 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0xe0); |
| 421 | |
| 422 | done: |
| 423 | return slot; |
| 424 | } |
| 425 | |
| 426 | static int bcm_qspi_transfer_one(struct spi_master *master, |
| 427 | struct spi_device *spi, |
| 428 | struct spi_transfer *trans) |
| 429 | { |
| 430 | struct bcm_qspi *qspi = spi_master_get_devdata(master); |
| 431 | int slots; |
| 432 | unsigned long timeo = msecs_to_jiffies(100); |
| 433 | |
| 434 | bcm_qspi_chip_select(qspi, spi->chip_select); |
| 435 | qspi->trans_pos.trans = trans; |
| 436 | qspi->trans_pos.byte = 0; |
| 437 | |
| 438 | while (qspi->trans_pos.byte < trans->len) { |
| 439 | reinit_completion(&qspi->mspi_done); |
| 440 | |
| 441 | slots = write_to_hw(qspi, spi); |
| 442 | if (!wait_for_completion_timeout(&qspi->mspi_done, timeo)) { |
| 443 | dev_err(&qspi->pdev->dev, "timeout waiting for MSPI\n"); |
| 444 | return -ETIMEDOUT; |
| 445 | } |
| 446 | |
| 447 | read_from_hw(qspi, slots); |
| 448 | } |
| 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static void bcm_qspi_cleanup(struct spi_device *spi) |
| 454 | { |
| 455 | struct bcm_qspi_parms *xp = spi_get_ctldata(spi); |
| 456 | |
| 457 | kfree(xp); |
| 458 | } |
| 459 | |
| 460 | static irqreturn_t bcm_qspi_mspi_l2_isr(int irq, void *dev_id) |
| 461 | { |
| 462 | struct bcm_qspi_dev_id *qspi_dev_id = dev_id; |
| 463 | struct bcm_qspi *qspi = qspi_dev_id->dev; |
| 464 | u32 status = bcm_qspi_read(qspi, MSPI, MSPI_MSPI_STATUS); |
| 465 | |
| 466 | if (status & MSPI_MSPI_STATUS_SPIF) { |
| 467 | /* clear interrupt */ |
| 468 | status &= ~MSPI_MSPI_STATUS_SPIF; |
| 469 | bcm_qspi_write(qspi, MSPI, MSPI_MSPI_STATUS, status); |
| 470 | complete(&qspi->mspi_done); |
| 471 | return IRQ_HANDLED; |
| 472 | } else { |
| 473 | return IRQ_NONE; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | static const struct bcm_qspi_irq qspi_irq_tab[] = { |
| 478 | { |
| 479 | .irq_name = "mspi_done", |
| 480 | .irq_handler = bcm_qspi_mspi_l2_isr, |
| 481 | .mask = INTR_MSPI_DONE_MASK, |
| 482 | }, |
| 483 | { |
| 484 | .irq_name = "mspi_halted", |
| 485 | .irq_handler = bcm_qspi_mspi_l2_isr, |
| 486 | .mask = INTR_MSPI_HALTED_MASK, |
| 487 | }, |
| 488 | }; |
| 489 | |
| 490 | static void bcm_qspi_hw_init(struct bcm_qspi *qspi) |
| 491 | { |
| 492 | struct bcm_qspi_parms parms; |
| 493 | |
| 494 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR1_LSB, 0); |
| 495 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR1_MSB, 0); |
| 496 | bcm_qspi_write(qspi, MSPI, MSPI_NEWQP, 0); |
| 497 | bcm_qspi_write(qspi, MSPI, MSPI_ENDQP, 0); |
| 498 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0x20); |
| 499 | |
| 500 | parms.mode = SPI_MODE_3; |
| 501 | parms.bits_per_word = 8; |
| 502 | parms.speed_hz = qspi->max_speed_hz; |
| 503 | bcm_qspi_hw_set_parms(qspi, &parms); |
| 504 | } |
| 505 | |
| 506 | static void bcm_qspi_hw_uninit(struct bcm_qspi *qspi) |
| 507 | { |
| 508 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0); |
| 509 | } |
| 510 | |
| 511 | static const struct of_device_id bcm_qspi_of_match[] = { |
| 512 | { .compatible = "brcm,spi-bcm-qspi" }, |
| 513 | {}, |
| 514 | }; |
| 515 | MODULE_DEVICE_TABLE(of, bcm_qspi_of_match); |
| 516 | |
| 517 | int bcm_qspi_probe(struct platform_device *pdev, |
| 518 | struct bcm_qspi_soc_intc *soc) |
| 519 | { |
| 520 | struct device *dev = &pdev->dev; |
| 521 | struct bcm_qspi *qspi; |
| 522 | struct spi_master *master; |
| 523 | struct resource *res; |
| 524 | int irq, ret = 0, num_ints = 0; |
| 525 | u32 val; |
| 526 | const char *name = NULL; |
| 527 | int num_irqs = ARRAY_SIZE(qspi_irq_tab); |
| 528 | |
| 529 | /* We only support device-tree instantiation */ |
| 530 | if (!dev->of_node) |
| 531 | return -ENODEV; |
| 532 | |
| 533 | if (!of_match_node(bcm_qspi_of_match, dev->of_node)) |
| 534 | return -ENODEV; |
| 535 | |
| 536 | master = spi_alloc_master(dev, sizeof(struct bcm_qspi)); |
| 537 | if (!master) { |
| 538 | dev_err(dev, "error allocating spi_master\n"); |
| 539 | return -ENOMEM; |
| 540 | } |
| 541 | |
| 542 | qspi = spi_master_get_devdata(master); |
| 543 | qspi->pdev = pdev; |
| 544 | qspi->trans_pos.trans = NULL; |
| 545 | qspi->trans_pos.byte = 0; |
| 546 | qspi->master = master; |
| 547 | |
| 548 | master->bus_num = -1; |
| 549 | master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_RX_DUAL | SPI_RX_QUAD; |
| 550 | master->setup = bcm_qspi_setup; |
| 551 | master->transfer_one = bcm_qspi_transfer_one; |
| 552 | master->cleanup = bcm_qspi_cleanup; |
| 553 | master->dev.of_node = dev->of_node; |
| 554 | master->num_chipselect = NUM_CHIPSELECT; |
| 555 | |
| 556 | qspi->big_endian = of_device_is_big_endian(dev->of_node); |
| 557 | |
| 558 | if (!of_property_read_u32(dev->of_node, "num-cs", &val)) |
| 559 | master->num_chipselect = val; |
| 560 | |
| 561 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hif_mspi"); |
| 562 | if (!res) |
| 563 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 564 | "mspi"); |
| 565 | |
| 566 | if (res) { |
| 567 | qspi->base[MSPI] = devm_ioremap_resource(dev, res); |
| 568 | if (IS_ERR(qspi->base[MSPI])) { |
| 569 | ret = PTR_ERR(qspi->base[MSPI]); |
| 570 | goto qspi_probe_err; |
| 571 | } |
| 572 | } else { |
| 573 | goto qspi_probe_err; |
| 574 | } |
| 575 | |
| 576 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs_reg"); |
| 577 | if (res) { |
| 578 | qspi->base[CHIP_SELECT] = devm_ioremap_resource(dev, res); |
| 579 | if (IS_ERR(qspi->base[CHIP_SELECT])) { |
| 580 | ret = PTR_ERR(qspi->base[CHIP_SELECT]); |
| 581 | goto qspi_probe_err; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | qspi->dev_ids = kcalloc(num_irqs, sizeof(struct bcm_qspi_dev_id), |
| 586 | GFP_KERNEL); |
| 587 | if (IS_ERR(qspi->dev_ids)) { |
| 588 | ret = PTR_ERR(qspi->dev_ids); |
| 589 | goto qspi_probe_err; |
| 590 | } |
| 591 | |
| 592 | for (val = 0; val < num_irqs; val++) { |
| 593 | irq = -1; |
| 594 | name = qspi_irq_tab[val].irq_name; |
| 595 | irq = platform_get_irq_byname(pdev, name); |
| 596 | |
| 597 | if (irq >= 0) { |
| 598 | ret = devm_request_irq(&pdev->dev, irq, |
| 599 | qspi_irq_tab[val].irq_handler, 0, |
| 600 | name, |
| 601 | &qspi->dev_ids[val]); |
| 602 | if (ret < 0) { |
| 603 | dev_err(&pdev->dev, "IRQ %s not found\n", name); |
| 604 | goto qspi_probe_err; |
| 605 | } |
| 606 | |
| 607 | qspi->dev_ids[val].dev = qspi; |
| 608 | qspi->dev_ids[val].irqp = &qspi_irq_tab[val]; |
| 609 | num_ints++; |
| 610 | dev_dbg(&pdev->dev, "registered IRQ %s %d\n", |
| 611 | qspi_irq_tab[val].irq_name, |
| 612 | irq); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | if (!num_ints) { |
| 617 | dev_err(&pdev->dev, "no IRQs registered, cannot init driver\n"); |
| 618 | goto qspi_probe_err; |
| 619 | } |
| 620 | |
| 621 | qspi->clk = devm_clk_get(&pdev->dev, NULL); |
| 622 | if (IS_ERR(qspi->clk)) { |
| 623 | dev_warn(dev, "unable to get clock\n"); |
| 624 | goto qspi_probe_err; |
| 625 | } |
| 626 | |
| 627 | ret = clk_prepare_enable(qspi->clk); |
| 628 | if (ret) { |
| 629 | dev_err(dev, "failed to prepare clock\n"); |
| 630 | goto qspi_probe_err; |
| 631 | } |
| 632 | |
| 633 | qspi->base_clk = clk_get_rate(qspi->clk); |
| 634 | qspi->max_speed_hz = qspi->base_clk / (QSPI_SPBR_MIN * 2); |
| 635 | |
| 636 | bcm_qspi_hw_init(qspi); |
| 637 | init_completion(&qspi->mspi_done); |
| 638 | qspi->curr_cs = -1; |
| 639 | |
| 640 | platform_set_drvdata(pdev, qspi); |
| 641 | ret = devm_spi_register_master(&pdev->dev, master); |
| 642 | if (ret < 0) { |
| 643 | dev_err(dev, "can't register master\n"); |
| 644 | goto qspi_reg_err; |
| 645 | } |
| 646 | |
| 647 | return 0; |
| 648 | |
| 649 | qspi_reg_err: |
| 650 | bcm_qspi_hw_uninit(qspi); |
| 651 | clk_disable_unprepare(qspi->clk); |
| 652 | qspi_probe_err: |
| 653 | spi_master_put(master); |
| 654 | kfree(qspi->dev_ids); |
| 655 | return ret; |
| 656 | } |
| 657 | /* probe function to be called by SoC specific platform driver probe */ |
| 658 | EXPORT_SYMBOL_GPL(bcm_qspi_probe); |
| 659 | |
| 660 | int bcm_qspi_remove(struct platform_device *pdev) |
| 661 | { |
| 662 | struct bcm_qspi *qspi = platform_get_drvdata(pdev); |
| 663 | |
| 664 | platform_set_drvdata(pdev, NULL); |
| 665 | bcm_qspi_hw_uninit(qspi); |
| 666 | clk_disable_unprepare(qspi->clk); |
| 667 | kfree(qspi->dev_ids); |
| 668 | spi_unregister_master(qspi->master); |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | /* function to be called by SoC specific platform driver remove() */ |
| 673 | EXPORT_SYMBOL_GPL(bcm_qspi_remove); |
| 674 | |
| 675 | #ifdef CONFIG_PM_SLEEP |
| 676 | static int bcm_qspi_suspend(struct device *dev) |
| 677 | { |
| 678 | struct bcm_qspi *qspi = dev_get_drvdata(dev); |
| 679 | |
| 680 | spi_master_suspend(qspi->master); |
| 681 | clk_disable(qspi->clk); |
| 682 | bcm_qspi_hw_uninit(qspi); |
| 683 | |
| 684 | return 0; |
| 685 | }; |
| 686 | |
| 687 | static int bcm_qspi_resume(struct device *dev) |
| 688 | { |
| 689 | struct bcm_qspi *qspi = dev_get_drvdata(dev); |
| 690 | int ret = 0; |
| 691 | |
| 692 | bcm_qspi_hw_init(qspi); |
| 693 | bcm_qspi_chip_select(qspi, qspi->curr_cs); |
| 694 | ret = clk_enable(qspi->clk); |
| 695 | if (!ret) |
| 696 | spi_master_resume(qspi->master); |
| 697 | |
| 698 | return ret; |
| 699 | } |
| 700 | #endif /* CONFIG_PM_SLEEP */ |
| 701 | |
| 702 | const struct dev_pm_ops bcm_qspi_pm_ops = { |
| 703 | .suspend = bcm_qspi_suspend, |
| 704 | .resume = bcm_qspi_resume, |
| 705 | }; |
| 706 | /* pm_ops to be called by SoC specific platform driver */ |
| 707 | EXPORT_SYMBOL_GPL(bcm_qspi_pm_ops); |
| 708 | |
| 709 | MODULE_AUTHOR("Kamal Dasu"); |
| 710 | MODULE_DESCRIPTION("Broadcom QSPI driver"); |
| 711 | MODULE_LICENSE("GPL v2"); |
| 712 | MODULE_ALIAS("platform:" DRIVER_NAME); |