static int __devinit xilinx_spi_of_probe(struct of_device *ofdev, const struct of_device_id *match) { struct spi_master *master; struct xspi_platform_data *pdata; struct resource r_mem; struct resource r_irq; int rc = 0; const u32 *prop; int len; rc = of_address_to_resource(ofdev->node, 0, &r_mem); if (rc) { dev_warn(&ofdev->dev, "invalid address\n"); return rc; } rc = of_irq_to_resource(ofdev->node, 0, &r_irq); if (rc == NO_IRQ) { dev_warn(&ofdev->dev, "no IRQ found\n"); return -ENODEV; } ofdev->dev.platform_data = kzalloc(sizeof(struct xspi_platform_data), GFP_KERNEL); pdata = ofdev->dev.platform_data; if (!pdata) return -ENOMEM; /* number of slave select bits is required */ prop = of_get_property(ofdev->node, "xlnx,num-ss-bits", &len); if (!prop || len < sizeof(*prop)) { dev_warn(&ofdev->dev, "no 'xlnx,num-ss-bits' property\n"); return -EINVAL; } pdata->num_chipselect = *prop; pdata->bits_per_word = 8; master = xilinx_spi_init(&ofdev->dev, &r_mem, r_irq.start, -1); if (!master) return -ENODEV; dev_set_drvdata(&ofdev->dev, master); /* Add any subnodes on the SPI bus */ of_register_spi_devices(master, ofdev->node); return 0; }
static int __init xilinx_spi_of_probe(struct of_device *ofdev, const struct of_device_id *match) { struct spi_master *master; struct xilinx_spi *xspi; struct resource r_irq_struct; struct resource r_mem_struct; struct resource *r_irq = &r_irq_struct; struct resource *r_mem = &r_mem_struct; int rc = 0; const u32 *prop; int len; /* Get resources(memory, IRQ) associated with the device */ master = spi_alloc_master(&ofdev->dev, sizeof(struct xilinx_spi)); if (master == NULL) { return -ENOMEM; } dev_set_drvdata(&ofdev->dev, master); rc = of_address_to_resource(ofdev->node, 0, r_mem); if (rc) { dev_warn(&ofdev->dev, "invalid address\n"); goto put_master; } rc = of_irq_to_resource(ofdev->node, 0, r_irq); if (rc == NO_IRQ) { dev_warn(&ofdev->dev, "no IRQ found\n"); goto put_master; } xspi = spi_master_get_devdata(master); xspi->bitbang.master = spi_master_get(master); xspi->bitbang.chipselect = xilinx_spi_chipselect; xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer; xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs; xspi->bitbang.master->setup = xilinx_spi_setup; init_completion(&xspi->done); xspi->irq = r_irq->start; if (!request_mem_region(r_mem->start, r_mem->end - r_mem->start + 1, XILINX_SPI_NAME)) { rc = -ENXIO; dev_warn(&ofdev->dev, "memory request failure\n"); goto put_master; } xspi->regs = ioremap(r_mem->start, r_mem->end - r_mem->start + 1); if (xspi->regs == NULL) { rc = -ENOMEM; dev_warn(&ofdev->dev, "ioremap failure\n"); goto put_master; } xspi->irq = r_irq->start; /* dynamic bus assignment */ master->bus_num = -1; /* number of slave select bits is required */ prop = of_get_property(ofdev->node, "xlnx,num-ss-bits", &len); if (!prop || len < sizeof(*prop)) { dev_warn(&ofdev->dev, "no 'xlnx,num-ss-bits' property\n"); goto put_master; } master->num_chipselect = *prop; /* SPI controller initializations */ xspi_init_hw(xspi->regs); /* Register for SPI Interrupt */ rc = request_irq(xspi->irq, xilinx_spi_irq, 0, XILINX_SPI_NAME, xspi); if (rc != 0) { dev_warn(&ofdev->dev, "irq request failure: %d\n", xspi->irq); goto unmap_io; } rc = spi_bitbang_start(&xspi->bitbang); if (rc != 0) { dev_err(&ofdev->dev, "spi_bitbang_start FAILED\n"); goto free_irq; } dev_info(&ofdev->dev, "at 0x%08X mapped to 0x%08X, irq=%d\n", (unsigned int)r_mem->start, (u32)xspi->regs, xspi->irq); /* Add any subnodes on the SPI bus */ of_register_spi_devices(master, ofdev->node); return rc; free_irq: free_irq(xspi->irq, xspi); unmap_io: iounmap(xspi->regs); put_master: spi_master_put(master); return rc; }
/* * OF Platform Bus Binding */ static int __devinit mpc52xx_spi_probe(struct of_device *op, const struct of_device_id *match) { struct spi_master *master; struct mpc52xx_spi *ms; void __iomem *regs; u8 ctrl1; int rc, i = 0; int gpio_cs; /* MMIO registers */ dev_dbg(&op->dev, "probing mpc5200 SPI device\n"); regs = of_iomap(op->node, 0); if (!regs) return -ENODEV; /* initialize the device */ ctrl1 = SPI_CTRL1_SPIE | SPI_CTRL1_SPE | SPI_CTRL1_MSTR; out_8(regs + SPI_CTRL1, ctrl1); out_8(regs + SPI_CTRL2, 0x0); out_8(regs + SPI_DATADIR, 0xe); /* Set output pins */ out_8(regs + SPI_PORTDATA, 0x8); /* Deassert /SS signal */ /* Clear the status register and re-read it to check for a MODF * failure. This driver cannot currently handle multiple masters * on the SPI bus. This fault will also occur if the SPI signals * are not connected to any pins (port_config setting) */ in_8(regs + SPI_STATUS); out_8(regs + SPI_CTRL1, ctrl1); in_8(regs + SPI_DATA); if (in_8(regs + SPI_STATUS) & SPI_STATUS_MODF) { dev_err(&op->dev, "mode fault; is port_config correct?\n"); rc = -EIO; goto err_init; } dev_dbg(&op->dev, "allocating spi_master struct\n"); master = spi_alloc_master(&op->dev, sizeof *ms); if (!master) { rc = -ENOMEM; goto err_alloc; } master->bus_num = -1; master->setup = mpc52xx_spi_setup; master->transfer = mpc52xx_spi_transfer; master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST; dev_set_drvdata(&op->dev, master); ms = spi_master_get_devdata(master); ms->master = master; ms->regs = regs; ms->irq0 = irq_of_parse_and_map(op->node, 0); ms->irq1 = irq_of_parse_and_map(op->node, 1); ms->state = mpc52xx_spi_fsmstate_idle; ms->ipb_freq = mpc5xxx_get_bus_frequency(op->node); ms->gpio_cs_count = of_gpio_count(op->node); if (ms->gpio_cs_count > 0) { master->num_chipselect = ms->gpio_cs_count; ms->gpio_cs = kmalloc(ms->gpio_cs_count * sizeof(unsigned int), GFP_KERNEL); if (!ms->gpio_cs) { rc = -ENOMEM; goto err_alloc; } for (i = 0; i < ms->gpio_cs_count; i++) { gpio_cs = of_get_gpio(op->node, i); if (gpio_cs < 0) { dev_err(&op->dev, "could not parse the gpio field " "in oftree\n"); rc = -ENODEV; goto err_gpio; } rc = gpio_request(gpio_cs, dev_name(&op->dev)); if (rc) { dev_err(&op->dev, "can't request spi cs gpio #%d " "on gpio line %d\n", i, gpio_cs); goto err_gpio; } gpio_direction_output(gpio_cs, 1); ms->gpio_cs[i] = gpio_cs; } } else { master->num_chipselect = 1; } spin_lock_init(&ms->lock); INIT_LIST_HEAD(&ms->queue); INIT_WORK(&ms->work, mpc52xx_spi_wq); /* Decide if interrupts can be used */ if (ms->irq0 && ms->irq1) { rc = request_irq(ms->irq0, mpc52xx_spi_irq, 0, "mpc5200-spi-modf", ms); rc |= request_irq(ms->irq1, mpc52xx_spi_irq, 0, "mpc5200-spi-spif", ms); if (rc) { free_irq(ms->irq0, ms); free_irq(ms->irq1, ms); ms->irq0 = ms->irq1 = 0; } } else { /* operate in polled mode */ ms->irq0 = ms->irq1 = 0; } if (!ms->irq0) dev_info(&op->dev, "using polled mode\n"); dev_dbg(&op->dev, "registering spi_master struct\n"); rc = spi_register_master(master); if (rc) goto err_register; of_register_spi_devices(master, op->node); dev_info(&ms->master->dev, "registered MPC5200 SPI bus\n"); return rc; err_register: dev_err(&ms->master->dev, "initialization failed\n"); spi_master_put(master); err_gpio: while (i-- > 0) gpio_free(ms->gpio_cs[i]); kfree(ms->gpio_cs); err_alloc: err_init: iounmap(regs); return rc; }