Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * at91_cf.c -- AT91 CompactFlash controller driver
0004  *
0005  * Copyright (C) 2005 David Brownell
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/kernel.h>
0010 #include <linux/platform_device.h>
0011 #include <linux/errno.h>
0012 #include <linux/init.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/slab.h>
0015 #include <linux/gpio.h>
0016 #include <linux/io.h>
0017 #include <linux/sizes.h>
0018 #include <linux/mfd/syscon.h>
0019 #include <linux/mfd/syscon/atmel-mc.h>
0020 #include <linux/of.h>
0021 #include <linux/of_device.h>
0022 #include <linux/of_gpio.h>
0023 #include <linux/pci.h>
0024 #include <linux/regmap.h>
0025 
0026 #include <pcmcia/ss.h>
0027 
0028 /*
0029  * A0..A10 work in each range; A23 indicates I/O space;  A25 is CFRNW;
0030  * some other bit in {A24,A22..A11} is nREG to flag memory access
0031  * (vs attributes).  So more than 2KB/region would just be waste.
0032  * Note: These are offsets from the physical base address.
0033  */
0034 #define CF_ATTR_PHYS    (0)
0035 #define CF_IO_PHYS  (1 << 23)
0036 #define CF_MEM_PHYS (0x017ff800)
0037 
0038 struct at91_cf_data {
0039     int irq_pin;        /* I/O IRQ */
0040     int det_pin;        /* Card detect */
0041     int vcc_pin;        /* power switching */
0042     int rst_pin;        /* card reset */
0043     u8  chipselect;     /* EBI Chip Select number */
0044     u8  flags;
0045 #define AT91_CF_TRUE_IDE    0x01
0046 #define AT91_IDE_SWAP_A0_A2 0x02
0047 };
0048 
0049 struct regmap *mc;
0050 
0051 /*--------------------------------------------------------------------------*/
0052 
0053 struct at91_cf_socket {
0054     struct pcmcia_socket    socket;
0055 
0056     unsigned        present:1;
0057 
0058     struct platform_device  *pdev;
0059     struct at91_cf_data *board;
0060 
0061     unsigned long       phys_baseaddr;
0062 };
0063 
0064 static inline int at91_cf_present(struct at91_cf_socket *cf)
0065 {
0066     return !gpio_get_value(cf->board->det_pin);
0067 }
0068 
0069 /*--------------------------------------------------------------------------*/
0070 
0071 static int at91_cf_ss_init(struct pcmcia_socket *s)
0072 {
0073     return 0;
0074 }
0075 
0076 static irqreturn_t at91_cf_irq(int irq, void *_cf)
0077 {
0078     struct at91_cf_socket *cf = _cf;
0079 
0080     if (irq == gpio_to_irq(cf->board->det_pin)) {
0081         unsigned present = at91_cf_present(cf);
0082 
0083         /* kick pccard as needed */
0084         if (present != cf->present) {
0085             cf->present = present;
0086             dev_dbg(&cf->pdev->dev, "card %s\n",
0087                     present ? "present" : "gone");
0088             pcmcia_parse_events(&cf->socket, SS_DETECT);
0089         }
0090     }
0091 
0092     return IRQ_HANDLED;
0093 }
0094 
0095 static int at91_cf_get_status(struct pcmcia_socket *s, u_int *sp)
0096 {
0097     struct at91_cf_socket   *cf;
0098 
0099     if (!sp)
0100         return -EINVAL;
0101 
0102     cf = container_of(s, struct at91_cf_socket, socket);
0103 
0104     /* NOTE: CF is always 3VCARD */
0105     if (at91_cf_present(cf)) {
0106         int rdy = gpio_is_valid(cf->board->irq_pin);    /* RDY/nIRQ */
0107         int vcc = gpio_is_valid(cf->board->vcc_pin);
0108 
0109         *sp = SS_DETECT | SS_3VCARD;
0110         if (!rdy || gpio_get_value(cf->board->irq_pin))
0111             *sp |= SS_READY;
0112         if (!vcc || gpio_get_value(cf->board->vcc_pin))
0113             *sp |= SS_POWERON;
0114     } else
0115         *sp = 0;
0116 
0117     return 0;
0118 }
0119 
0120 static int
0121 at91_cf_set_socket(struct pcmcia_socket *sock, struct socket_state_t *s)
0122 {
0123     struct at91_cf_socket   *cf;
0124 
0125     cf = container_of(sock, struct at91_cf_socket, socket);
0126 
0127     /* switch Vcc if needed and possible */
0128     if (gpio_is_valid(cf->board->vcc_pin)) {
0129         switch (s->Vcc) {
0130         case 0:
0131             gpio_set_value(cf->board->vcc_pin, 0);
0132             break;
0133         case 33:
0134             gpio_set_value(cf->board->vcc_pin, 1);
0135             break;
0136         default:
0137             return -EINVAL;
0138         }
0139     }
0140 
0141     /* toggle reset if needed */
0142     gpio_set_value(cf->board->rst_pin, s->flags & SS_RESET);
0143 
0144     dev_dbg(&cf->pdev->dev, "Vcc %d, io_irq %d, flags %04x csc %04x\n",
0145                 s->Vcc, s->io_irq, s->flags, s->csc_mask);
0146 
0147     return 0;
0148 }
0149 
0150 static int at91_cf_ss_suspend(struct pcmcia_socket *s)
0151 {
0152     return at91_cf_set_socket(s, &dead_socket);
0153 }
0154 
0155 /* we already mapped the I/O region */
0156 static int at91_cf_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
0157 {
0158     struct at91_cf_socket   *cf;
0159     u32         csr;
0160 
0161     cf = container_of(s, struct at91_cf_socket, socket);
0162     io->flags &= (MAP_ACTIVE | MAP_16BIT | MAP_AUTOSZ);
0163 
0164     /*
0165      * Use 16 bit accesses unless/until we need 8-bit i/o space.
0166      *
0167      * NOTE: this CF controller ignores IOIS16, so we can't really do
0168      * MAP_AUTOSZ.  The 16bit mode allows single byte access on either
0169      * D0-D7 (even addr) or D8-D15 (odd), so it's close enough for many
0170      * purposes (and handles ide-cs).
0171      *
0172      * The 8bit mode is needed for odd byte access on D0-D7.  It seems
0173      * some cards only like that way to get at the odd byte, despite
0174      * CF 3.0 spec table 35 also giving the D8-D15 option.
0175      */
0176     if (!(io->flags & (MAP_16BIT | MAP_AUTOSZ))) {
0177         csr = AT91_MC_SMC_DBW_8;
0178         dev_dbg(&cf->pdev->dev, "8bit i/o bus\n");
0179     } else {
0180         csr = AT91_MC_SMC_DBW_16;
0181         dev_dbg(&cf->pdev->dev, "16bit i/o bus\n");
0182     }
0183     regmap_update_bits(mc, AT91_MC_SMC_CSR(cf->board->chipselect),
0184                AT91_MC_SMC_DBW, csr);
0185 
0186     io->start = cf->socket.io_offset;
0187     io->stop = io->start + SZ_2K - 1;
0188 
0189     return 0;
0190 }
0191 
0192 /* pcmcia layer maps/unmaps mem regions */
0193 static int
0194 at91_cf_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *map)
0195 {
0196     struct at91_cf_socket   *cf;
0197 
0198     if (map->card_start)
0199         return -EINVAL;
0200 
0201     cf = container_of(s, struct at91_cf_socket, socket);
0202 
0203     map->flags &= (MAP_ACTIVE | MAP_ATTRIB | MAP_16BIT);
0204     if (map->flags & MAP_ATTRIB)
0205         map->static_start = cf->phys_baseaddr + CF_ATTR_PHYS;
0206     else
0207         map->static_start = cf->phys_baseaddr + CF_MEM_PHYS;
0208 
0209     return 0;
0210 }
0211 
0212 static struct pccard_operations at91_cf_ops = {
0213     .init           = at91_cf_ss_init,
0214     .suspend        = at91_cf_ss_suspend,
0215     .get_status     = at91_cf_get_status,
0216     .set_socket     = at91_cf_set_socket,
0217     .set_io_map     = at91_cf_set_io_map,
0218     .set_mem_map        = at91_cf_set_mem_map,
0219 };
0220 
0221 /*--------------------------------------------------------------------------*/
0222 
0223 static const struct of_device_id at91_cf_dt_ids[] = {
0224     { .compatible = "atmel,at91rm9200-cf" },
0225     { /* sentinel */ }
0226 };
0227 MODULE_DEVICE_TABLE(of, at91_cf_dt_ids);
0228 
0229 static int at91_cf_probe(struct platform_device *pdev)
0230 {
0231     struct at91_cf_socket   *cf;
0232     struct at91_cf_data *board;
0233     struct resource     *io;
0234     struct resource     realio;
0235     int         status;
0236 
0237     board = devm_kzalloc(&pdev->dev, sizeof(*board), GFP_KERNEL);
0238     if (!board)
0239         return -ENOMEM;
0240 
0241     board->irq_pin = of_get_gpio(pdev->dev.of_node, 0);
0242     board->det_pin = of_get_gpio(pdev->dev.of_node, 1);
0243     board->vcc_pin = of_get_gpio(pdev->dev.of_node, 2);
0244     board->rst_pin = of_get_gpio(pdev->dev.of_node, 3);
0245 
0246     mc = syscon_regmap_lookup_by_compatible("atmel,at91rm9200-sdramc");
0247     if (IS_ERR(mc))
0248         return PTR_ERR(mc);
0249 
0250     if (!gpio_is_valid(board->det_pin) || !gpio_is_valid(board->rst_pin))
0251         return -ENODEV;
0252 
0253     io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0254     if (!io)
0255         return -ENODEV;
0256 
0257     cf = devm_kzalloc(&pdev->dev, sizeof(*cf), GFP_KERNEL);
0258     if (!cf)
0259         return -ENOMEM;
0260 
0261     cf->board = board;
0262     cf->pdev = pdev;
0263     cf->phys_baseaddr = io->start;
0264     platform_set_drvdata(pdev, cf);
0265 
0266     /* must be a GPIO; ergo must trigger on both edges */
0267     status = devm_gpio_request(&pdev->dev, board->det_pin, "cf_det");
0268     if (status < 0)
0269         return status;
0270 
0271     status = devm_request_irq(&pdev->dev, gpio_to_irq(board->det_pin),
0272                     at91_cf_irq, 0, "at91_cf detect", cf);
0273     if (status < 0)
0274         return status;
0275 
0276     device_init_wakeup(&pdev->dev, 1);
0277 
0278     status = devm_gpio_request(&pdev->dev, board->rst_pin, "cf_rst");
0279     if (status < 0)
0280         goto fail0a;
0281 
0282     if (gpio_is_valid(board->vcc_pin)) {
0283         status = devm_gpio_request(&pdev->dev, board->vcc_pin, "cf_vcc");
0284         if (status < 0)
0285             goto fail0a;
0286     }
0287 
0288     /*
0289      * The card driver will request this irq later as needed.
0290      * but it causes lots of "irqNN: nobody cared" messages
0291      * unless we report that we handle everything (sigh).
0292      * (Note:  DK board doesn't wire the IRQ pin...)
0293      */
0294     if (gpio_is_valid(board->irq_pin)) {
0295         status = devm_gpio_request(&pdev->dev, board->irq_pin, "cf_irq");
0296         if (status < 0)
0297             goto fail0a;
0298 
0299         status = devm_request_irq(&pdev->dev, gpio_to_irq(board->irq_pin),
0300                     at91_cf_irq, IRQF_SHARED, "at91_cf", cf);
0301         if (status < 0)
0302             goto fail0a;
0303         cf->socket.pci_irq = gpio_to_irq(board->irq_pin);
0304     } else
0305         cf->socket.pci_irq = nr_irqs + 1;
0306 
0307     /*
0308      * pcmcia layer only remaps "real" memory not iospace
0309      * io_offset is set to 0x10000 to avoid the check in static_find_io().
0310      * */
0311     cf->socket.io_offset = 0x10000;
0312     realio.start = cf->socket.io_offset;
0313     realio.end = realio.start + SZ_64K - 1;
0314     status = pci_remap_iospace(&realio, cf->phys_baseaddr + CF_IO_PHYS);
0315     if (status)
0316         goto fail0a;
0317 
0318     /* reserve chip-select regions */
0319     if (!devm_request_mem_region(&pdev->dev, io->start, resource_size(io), "at91_cf")) {
0320         status = -ENXIO;
0321         goto fail0a;
0322     }
0323 
0324     dev_info(&pdev->dev, "irqs det #%d, io #%d\n",
0325         gpio_to_irq(board->det_pin), gpio_to_irq(board->irq_pin));
0326 
0327     cf->socket.owner = THIS_MODULE;
0328     cf->socket.dev.parent = &pdev->dev;
0329     cf->socket.ops = &at91_cf_ops;
0330     cf->socket.resource_ops = &pccard_static_ops;
0331     cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP
0332                 | SS_CAP_MEM_ALIGN;
0333     cf->socket.map_size = SZ_2K;
0334     cf->socket.io[0].res = io;
0335 
0336     status = pcmcia_register_socket(&cf->socket);
0337     if (status < 0)
0338         goto fail0a;
0339 
0340     return 0;
0341 
0342 fail0a:
0343     device_init_wakeup(&pdev->dev, 0);
0344     return status;
0345 }
0346 
0347 static int at91_cf_remove(struct platform_device *pdev)
0348 {
0349     struct at91_cf_socket   *cf = platform_get_drvdata(pdev);
0350 
0351     pcmcia_unregister_socket(&cf->socket);
0352     device_init_wakeup(&pdev->dev, 0);
0353 
0354     return 0;
0355 }
0356 
0357 #ifdef  CONFIG_PM
0358 
0359 static int at91_cf_suspend(struct platform_device *pdev, pm_message_t mesg)
0360 {
0361     struct at91_cf_socket   *cf = platform_get_drvdata(pdev);
0362     struct at91_cf_data *board = cf->board;
0363 
0364     if (device_may_wakeup(&pdev->dev)) {
0365         enable_irq_wake(gpio_to_irq(board->det_pin));
0366         if (gpio_is_valid(board->irq_pin))
0367             enable_irq_wake(gpio_to_irq(board->irq_pin));
0368     }
0369     return 0;
0370 }
0371 
0372 static int at91_cf_resume(struct platform_device *pdev)
0373 {
0374     struct at91_cf_socket   *cf = platform_get_drvdata(pdev);
0375     struct at91_cf_data *board = cf->board;
0376 
0377     if (device_may_wakeup(&pdev->dev)) {
0378         disable_irq_wake(gpio_to_irq(board->det_pin));
0379         if (gpio_is_valid(board->irq_pin))
0380             disable_irq_wake(gpio_to_irq(board->irq_pin));
0381     }
0382 
0383     return 0;
0384 }
0385 
0386 #else
0387 #define at91_cf_suspend     NULL
0388 #define at91_cf_resume      NULL
0389 #endif
0390 
0391 static struct platform_driver at91_cf_driver = {
0392     .driver = {
0393         .name       = "at91_cf",
0394         .of_match_table = at91_cf_dt_ids,
0395     },
0396     .probe      = at91_cf_probe,
0397     .remove     = at91_cf_remove,
0398     .suspend    = at91_cf_suspend,
0399     .resume     = at91_cf_resume,
0400 };
0401 
0402 module_platform_driver(at91_cf_driver);
0403 
0404 MODULE_DESCRIPTION("AT91 Compact Flash Driver");
0405 MODULE_AUTHOR("David Brownell");
0406 MODULE_LICENSE("GPL");
0407 MODULE_ALIAS("platform:at91_cf");