1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright 2011 Freescale Semiconductor, Inc 4 * 5 * Freescale Integrated Flash Controller 6 * 7 * Author: Dipen Dudhat <Dipen.Dudhat@freescale.com> 8 */ 9 #include <linux/module.h> 10 #include <linux/kernel.h> 11 #include <linux/compiler.h> 12 #include <linux/sched.h> 13 #include <linux/spinlock.h> 14 #include <linux/types.h> 15 #include <linux/slab.h> 16 #include <linux/io.h> 17 #include <linux/of.h> 18 #include <linux/of_platform.h> 19 #include <linux/platform_device.h> 20 #include <linux/fsl_ifc.h> 21 #include <linux/irqdomain.h> 22 #include <linux/of_address.h> 23 #include <linux/of_irq.h> 24 25 struct fsl_ifc_ctrl *fsl_ifc_ctrl_dev; 26 EXPORT_SYMBOL(fsl_ifc_ctrl_dev); 27 28 /* 29 * convert_ifc_address - convert the base address 30 * @addr_base: base address of the memory bank 31 */ 32 unsigned int convert_ifc_address(phys_addr_t addr_base) 33 { 34 return addr_base & CSPR_BA; 35 } 36 EXPORT_SYMBOL(convert_ifc_address); 37 38 /* 39 * fsl_ifc_find - find IFC bank 40 * @addr_base: base address of the memory bank 41 * 42 * This function walks IFC banks comparing "Base address" field of the CSPR 43 * registers with the supplied addr_base argument. When bases match this 44 * function returns bank number (starting with 0), otherwise it returns 45 * appropriate errno value. 46 */ 47 int fsl_ifc_find(phys_addr_t addr_base) 48 { 49 int i = 0; 50 51 if (!fsl_ifc_ctrl_dev || !fsl_ifc_ctrl_dev->gregs) 52 return -ENODEV; 53 54 for (i = 0; i < fsl_ifc_ctrl_dev->banks; i++) { 55 u32 cspr = ifc_in32(&fsl_ifc_ctrl_dev->gregs->cspr_cs[i].cspr); 56 57 if (cspr & CSPR_V && (cspr & CSPR_BA) == 58 convert_ifc_address(addr_base)) 59 return i; 60 } 61 62 return -ENOENT; 63 } 64 EXPORT_SYMBOL(fsl_ifc_find); 65 66 static u32 fsl_ifc_ctrl_in32(struct fsl_ifc_ctrl *ctrl, void __iomem *addr) 67 { 68 if (ctrl->little_endian) 69 return ioread32(addr); 70 71 return ioread32be(addr); 72 } 73 74 static void fsl_ifc_ctrl_out32(struct fsl_ifc_ctrl *ctrl, u32 val, 75 void __iomem *addr) 76 { 77 if (ctrl->little_endian) 78 iowrite32(val, addr); 79 else 80 iowrite32be(val, addr); 81 } 82 83 static int fsl_ifc_ctrl_init(struct fsl_ifc_ctrl *ctrl) 84 { 85 struct fsl_ifc_global __iomem *ifc = ctrl->gregs; 86 87 /* 88 * Clear all the common status and event registers 89 */ 90 if (fsl_ifc_ctrl_in32(ctrl, &ifc->cm_evter_stat) & IFC_CM_EVTER_STAT_CSER) 91 fsl_ifc_ctrl_out32(ctrl, IFC_CM_EVTER_STAT_CSER, 92 &ifc->cm_evter_stat); 93 94 /* enable all error and events */ 95 fsl_ifc_ctrl_out32(ctrl, IFC_CM_EVTER_EN_CSEREN, &ifc->cm_evter_en); 96 97 /* enable all error and event interrupts */ 98 fsl_ifc_ctrl_out32(ctrl, IFC_CM_EVTER_INTR_EN_CSERIREN, 99 &ifc->cm_evter_intr_en); 100 fsl_ifc_ctrl_out32(ctrl, 0x0, &ifc->cm_erattr0); 101 fsl_ifc_ctrl_out32(ctrl, 0x0, &ifc->cm_erattr1); 102 103 return 0; 104 } 105 106 static void fsl_ifc_ctrl_remove(struct platform_device *dev) 107 { 108 struct fsl_ifc_ctrl *ctrl = dev_get_drvdata(&dev->dev); 109 110 of_platform_depopulate(&dev->dev); 111 if (ctrl->nand_irq) 112 free_irq(ctrl->nand_irq, ctrl); 113 free_irq(ctrl->irq, ctrl); 114 fsl_ifc_ctrl_dev = NULL; 115 116 irq_dispose_mapping(ctrl->nand_irq); 117 irq_dispose_mapping(ctrl->irq); 118 119 iounmap(ctrl->gregs); 120 121 dev_set_drvdata(&dev->dev, NULL); 122 } 123 124 /* 125 * NAND events are split between an operational interrupt which only 126 * receives OPC, and an error interrupt that receives everything else, 127 * including non-NAND errors. Whichever interrupt gets to it first 128 * records the status and wakes the wait queue. 129 */ 130 static DEFINE_SPINLOCK(nand_irq_lock); 131 132 static u32 check_nand_stat(struct fsl_ifc_ctrl *ctrl) 133 { 134 struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs; 135 unsigned long flags; 136 u32 stat; 137 138 spin_lock_irqsave(&nand_irq_lock, flags); 139 140 stat = ifc_in32(&ifc->ifc_nand.nand_evter_stat); 141 if (stat) { 142 ifc_out32(stat, &ifc->ifc_nand.nand_evter_stat); 143 ctrl->nand_stat = stat; 144 wake_up(&ctrl->nand_wait); 145 } 146 147 spin_unlock_irqrestore(&nand_irq_lock, flags); 148 149 return stat; 150 } 151 152 static irqreturn_t fsl_ifc_nand_irq(int irqno, void *data) 153 { 154 struct fsl_ifc_ctrl *ctrl = data; 155 156 if (check_nand_stat(ctrl)) 157 return IRQ_HANDLED; 158 159 return IRQ_NONE; 160 } 161 162 /* 163 * NOTE: This interrupt is used to report ifc events of various kinds, 164 * such as transaction errors on the chipselects. 165 */ 166 static irqreturn_t fsl_ifc_ctrl_irq(int irqno, void *data) 167 { 168 struct fsl_ifc_ctrl *ctrl = data; 169 struct fsl_ifc_global __iomem *ifc = ctrl->gregs; 170 u32 err_axiid, err_srcid, status, cs_err, err_addr; 171 irqreturn_t ret = IRQ_NONE; 172 173 /* read for chip select error */ 174 cs_err = ifc_in32(&ifc->cm_evter_stat); 175 if (cs_err) { 176 dev_err(ctrl->dev, "transaction sent to IFC is not mapped to any memory bank 0x%08X\n", 177 cs_err); 178 /* clear the chip select error */ 179 ifc_out32(IFC_CM_EVTER_STAT_CSER, &ifc->cm_evter_stat); 180 181 /* read error attribute registers print the error information */ 182 status = ifc_in32(&ifc->cm_erattr0); 183 err_addr = ifc_in32(&ifc->cm_erattr1); 184 185 if (status & IFC_CM_ERATTR0_ERTYP_READ) 186 dev_err(ctrl->dev, "Read transaction error CM_ERATTR0 0x%08X\n", 187 status); 188 else 189 dev_err(ctrl->dev, "Write transaction error CM_ERATTR0 0x%08X\n", 190 status); 191 192 err_axiid = (status & IFC_CM_ERATTR0_ERAID) >> 193 IFC_CM_ERATTR0_ERAID_SHIFT; 194 dev_err(ctrl->dev, "AXI ID of the error transaction 0x%08X\n", 195 err_axiid); 196 197 err_srcid = (status & IFC_CM_ERATTR0_ESRCID) >> 198 IFC_CM_ERATTR0_ESRCID_SHIFT; 199 dev_err(ctrl->dev, "SRC ID of the error transaction 0x%08X\n", 200 err_srcid); 201 202 dev_err(ctrl->dev, "Transaction Address corresponding to error ERADDR 0x%08X\n", 203 err_addr); 204 205 ret = IRQ_HANDLED; 206 } 207 208 if (check_nand_stat(ctrl)) 209 ret = IRQ_HANDLED; 210 211 return ret; 212 } 213 214 /* 215 * fsl_ifc_ctrl_probe 216 * 217 * called by device layer when it finds a device matching 218 * one our driver can handled. This code allocates all of 219 * the resources needed for the controller only. The 220 * resources for the NAND banks themselves are allocated 221 * in the chip probe function. 222 */ 223 static int fsl_ifc_ctrl_probe(struct platform_device *dev) 224 { 225 int ret = 0; 226 int version, banks; 227 void __iomem *addr; 228 struct fsl_ifc_ctrl *ctrl; 229 230 dev_info(&dev->dev, "Freescale Integrated Flash Controller\n"); 231 232 ctrl = devm_kzalloc_obj(&dev->dev, *ctrl); 233 if (!ctrl) 234 return -ENOMEM; 235 236 dev_set_drvdata(&dev->dev, ctrl); 237 238 /* IOMAP the entire IFC region */ 239 ctrl->gregs = of_iomap(dev->dev.of_node, 0); 240 if (!ctrl->gregs) { 241 dev_err(&dev->dev, "failed to get memory region\n"); 242 return -ENODEV; 243 } 244 245 if (of_property_read_bool(dev->dev.of_node, "little-endian")) { 246 ctrl->little_endian = true; 247 dev_dbg(&dev->dev, "IFC REGISTERS are LITTLE endian\n"); 248 } else { 249 ctrl->little_endian = false; 250 dev_dbg(&dev->dev, "IFC REGISTERS are BIG endian\n"); 251 } 252 253 version = fsl_ifc_ctrl_in32(ctrl, &ctrl->gregs->ifc_rev) & 254 FSL_IFC_VERSION_MASK; 255 256 banks = (version == FSL_IFC_VERSION_1_0_0) ? 4 : 8; 257 dev_info(&dev->dev, "IFC version %d.%d, %d banks\n", 258 version >> 24, (version >> 16) & 0xf, banks); 259 260 ctrl->version = version; 261 ctrl->banks = banks; 262 263 addr = ctrl->gregs; 264 if (version >= FSL_IFC_VERSION_2_0_0) 265 addr += PGOFFSET_64K; 266 else 267 addr += PGOFFSET_4K; 268 ctrl->rregs = addr; 269 270 /* get the Controller level irq */ 271 ctrl->irq = irq_of_parse_and_map(dev->dev.of_node, 0); 272 if (ctrl->irq == 0) { 273 dev_err(&dev->dev, "failed to get irq resource for IFC\n"); 274 ret = -ENODEV; 275 goto err; 276 } 277 278 /* get the nand machine irq */ 279 ctrl->nand_irq = irq_of_parse_and_map(dev->dev.of_node, 1); 280 281 ctrl->dev = &dev->dev; 282 283 ret = fsl_ifc_ctrl_init(ctrl); 284 if (ret < 0) 285 goto err_unmap_nandirq; 286 287 init_waitqueue_head(&ctrl->nand_wait); 288 289 fsl_ifc_ctrl_dev = ctrl; 290 291 ret = request_irq(ctrl->irq, fsl_ifc_ctrl_irq, IRQF_SHARED, 292 "fsl-ifc", ctrl); 293 if (ret != 0) { 294 dev_err(&dev->dev, "failed to install irq (%d)\n", 295 ctrl->irq); 296 goto err_clear_global; 297 } 298 299 if (ctrl->nand_irq) { 300 ret = request_irq(ctrl->nand_irq, fsl_ifc_nand_irq, 0, 301 "fsl-ifc-nand", ctrl); 302 if (ret != 0) { 303 dev_err(&dev->dev, "failed to install irq (%d)\n", 304 ctrl->nand_irq); 305 goto err_free_irq; 306 } 307 } 308 309 /* legacy dts may still use "simple-bus" compatible */ 310 ret = of_platform_default_populate(dev->dev.of_node, NULL, &dev->dev); 311 if (ret) 312 goto err_free_nandirq; 313 314 return 0; 315 316 err_free_nandirq: 317 if (ctrl->nand_irq) 318 free_irq(ctrl->nand_irq, ctrl); 319 err_free_irq: 320 free_irq(ctrl->irq, ctrl); 321 err_clear_global: 322 fsl_ifc_ctrl_dev = NULL; 323 err_unmap_nandirq: 324 irq_dispose_mapping(ctrl->nand_irq); 325 irq_dispose_mapping(ctrl->irq); 326 err: 327 iounmap(ctrl->gregs); 328 return ret; 329 } 330 331 static const struct of_device_id fsl_ifc_match[] = { 332 { 333 .compatible = "fsl,ifc", 334 }, 335 {}, 336 }; 337 338 static struct platform_driver fsl_ifc_ctrl_driver = { 339 .driver = { 340 .name = "fsl-ifc", 341 .of_match_table = fsl_ifc_match, 342 }, 343 .probe = fsl_ifc_ctrl_probe, 344 .remove = fsl_ifc_ctrl_remove, 345 }; 346 347 static int __init fsl_ifc_init(void) 348 { 349 return platform_driver_register(&fsl_ifc_ctrl_driver); 350 } 351 subsys_initcall(fsl_ifc_init); 352 353 MODULE_AUTHOR("Freescale Semiconductor"); 354 MODULE_DESCRIPTION("Freescale Integrated Flash Controller driver"); 355