1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * AMD SPI controller driver
4  *
5  * Copyright (c) 2025, Advanced Micro Devices, Inc.
6  * All Rights Reserved.
7  *
8  * Authors: Krishnamoorthi M <krishnamoorthi.m@amd.com>
9  *          Akshata MukundShetty <akshata.mukundshetty@amd.com>
10  */
11 
12 #include <linux/init.h>
13 #include <linux/spi/spi.h>
14 #include <linux/pci.h>
15 
16 #include "spi-amd.h"
17 
18 #define AMD_PCI_DEVICE_ID_LPC_BRIDGE		0x1682
19 #define AMD_PCI_LPC_SPI_BASE_ADDR_REG		0xA0
20 #define AMD_SPI_BASE_ADDR_MASK			~0xFF
21 #define AMD_HID2_PCI_BAR_OFFSET			0x00002000
22 #define AMD_HID2_MEM_SIZE			0x200
23 
24 static struct pci_device_id pci_spi_ids[] = {
25 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_PCI_DEVICE_ID_LPC_BRIDGE) },
26 	{ 0, }
27 };
28 MODULE_DEVICE_TABLE(pci, pci_spi_ids);
29 
30 static int amd_spi_pci_probe(struct pci_dev *pdev,
31 			     const struct pci_device_id *id)
32 {
33 	struct device *dev = &pdev->dev;
34 	struct spi_controller *host;
35 	struct amd_spi *amd_spi;
36 	u32 io_base_addr;
37 
38 	/* Allocate storage for host and driver private data */
39 	host = devm_spi_alloc_host(dev, sizeof(struct amd_spi));
40 	if (!host)
41 		return dev_err_probe(dev, -ENOMEM, "Error allocating SPI host\n");
42 
43 	amd_spi = spi_controller_get_devdata(host);
44 
45 	pci_read_config_dword(pdev, AMD_PCI_LPC_SPI_BASE_ADDR_REG, &io_base_addr);
46 	io_base_addr = (io_base_addr & AMD_SPI_BASE_ADDR_MASK) + AMD_HID2_PCI_BAR_OFFSET;
47 	amd_spi->io_remap_addr = devm_ioremap(dev, io_base_addr, AMD_HID2_MEM_SIZE);
48 
49 	if (!amd_spi->io_remap_addr)
50 		return dev_err_probe(dev, -ENOMEM,
51 				"ioremap of SPI registers failed\n");
52 
53 	dev_dbg(dev, "io_remap_address: %p\n", amd_spi->io_remap_addr);
54 
55 	amd_spi->version = AMD_HID2_SPI;
56 	host->bus_num = 2;
57 
58 	return amd_spi_probe_common(dev, host);
59 }
60 
61 static struct pci_driver amd_spi_pci_driver = {
62 	.name = "amd_spi_pci",
63 	.id_table = pci_spi_ids,
64 	.probe = amd_spi_pci_probe,
65 };
66 
67 module_pci_driver(amd_spi_pci_driver);
68 
69 MODULE_LICENSE("GPL");
70 MODULE_DESCRIPTION("AMD HID2 SPI Controller Driver");
71